0

I have a DataFrame composed of a column of prices

df = pd.DataFrame({"Price": [0, 1, 0, 5, 0]})

I want to add a new column called "Type" which lists whether the price is Free (i.e. zero) or Paid (non-zero)

df["Type"] = ["Paid" for x in df["Price"]] # Make all of them read as "Paid"
df["Type"] = ["Free" for x in df["Price"] if x == "0"] # Convert the Free apps to read as "Free"

The second line here is spitting out an error message.

ValueError: Length of values (3) does not match length of index (5)

I presume this means it is spitting out a column of only 3 points, so the "Paid" points are not allowed for and thus erroring out. How can I fix this?

Haris
  • 19
  • 2
  • Your solution `df["Type"] = ["Free" if x == 0 "Paid" else for x in df["Price"].to_numpy()]`, better `df["Type"] =np.where(df["Price"] == 0, 'Free','Paid')` – jezrael Apr 13 '22 at 05:10

0 Answers0