I am using this code. but instead of new with just the required rows, I'm getting an empty .csv with just the header.
import pandas as pd
df = pd.read_csv("E:/Mac&cheese.csv")
newdf = df[df["fruit"]=="watermelon" "*"]
newdf.to_csv("E:/Mac&cheese(2).csv",index=False)
CodePudding user response:
missing the underscore in pd.read_csv on first call, also it looks like the actual location is incorrect. missing the // in the file location.
CodePudding user response:
I believe the problem is in how you select the rows containing the word "watermelon". Instead of:
newdf = df[df["fruit"]=="watermelon" "*"]
Try:
newdf = df[df["fruit"].str.contains("watermelon")]
In your example, pandas is literally looking for cells containing the word "watermelon*".