0

I have a csv file pInfo.csv

id,state,rank,amount,area
74,GA,Bronze,298.62,7
263,FL,Diamond,421.43,60
33,IL,Silver,361.69,5

I am using pandas to try and write only all the files that contain rank of Silver to a new file.

So something like

df = pd.read_csv("pInfo.csv")
    if df['rank'].values == "Silver":
        df.to_csv('newInfo.csv')

I get an error "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

This is my first time working with pandas so any help or approach is appreciated.

JT Saiyan
  • 55
  • 1
  • 7
  • Just to be clear, you want to copy all rows with rank == 'Silver' only to a new CSV file, correct? – NYC Coder Oct 29 '20 at 16:47
  • yes so all rows rank of Silver output to new file would be something like id,state,rank,amount,area 74,GA,Silver,298.62,7 263,FL,Silver,421.43,60 33,IL,Silver,361.69,5 – JT Saiyan Oct 29 '20 at 16:48

2 Answers2

0

Just this should do:

df = df[df['rank'] == 'Silver']
df.to_csv('newInfo.csv', index=False)

CSV file:

id,state,rank,amount,area
33,IL,Silver,361.69,5
NYC Coder
  • 7,424
  • 2
  • 11
  • 24
0

You should use the .loc function:

df.loc[df['rank'] == 'Silver'].to_csv(my_filename, index=False)

works fine for me.

The reason for the error you got is that df['rank'].values == "Silver" returns an array of booleans. You cannot use if with an array like that. The suggestions in the error are any() or all(), which return True if any value in the array is True, or if all values are True, respectively.

Using the .loc[] functionality allows you to select only those rows (or columns) that satisfy the conditions you need.