-2

I'm trying to filter my csv file. I have 3 columns and the first one is Espacio. I have an error with my sentence so I don't understand how can I do it because I ever filter my csv with this:

import pandas as pd
df = pd.read_csv('my.csv')
if df['Espacio'] == '*':

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
  • Possible duplicate of [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Brad Solomon Mar 22 '18 at 14:51
  • it works for numbers but not for str. @BradSolomon –  Mar 22 '18 at 14:54

1 Answers1

1

Basically when you do

df = pd.read_csv('my.csv')

The datatype of df is 'dataframe' so you can't directly index elements out of it. What you need to do is use iterrows method to loop over the rows and then index the element you need.

To test this I created a simple csv file as below

Espacio,b,c
*,2,3
4,5,6

And then used this code

import pandas as pd
df = pd.read_csv('my.csv')

for index,row in df.iterrows():
    if row['Espacio'] == '*':
        print('here')

This should do what you need

Akshay
  • 783
  • 6
  • 20