0
votes

In a python pandas dataframe "user", I have the following two columns:

user_id | isorg
1       | 1
2       | 0
3       | 3  
4       | 0
5       | 0

I want itertuples() user_id with only isorg == 0, so i write

for row in user.itertuples():
  if row.isorg == 0: continue
  #action

But i get error like this

`--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in () 1 for row in user.itertuples(): ----> 2 if row.isorg == 0: continue 3 org = pd.DataFrame(m3twitter.infer_id(row.user_id)) 4 isorg = pd.DataFrame.from_dict(org.output.org, orient='index').T 5 isorg = pd.concat([isorg['is-org'].apply(pd.Series)])

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in nonzero(self) 1553 "The truth value of a {0} is ambiguous. " 1554 "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format( -> 1555 self.class.name 1556 ) 1557 )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().`

How to get right code? I newbie in python.

1
Could you show how the user dataframe is built? I can't reproduce the error.ferhen
path = r'/content/gdrive/My Drive/Data/' all_files = glob.glob(path + "/*.csv") li = [] for filename in all_files: df = pd.read_csv(filename, delimiter=';', engine='python',usecols=['user_id']).drop_duplicates(keep='first').reset_index() li.append(df) user = pd.concat(li, axis=0, ignore_index=True)Aan Andriatno
Can you post the result of print(user.to_dict()) here?ferhen
@ferhen He has drop_duplicates() in that read_csv() call (in the comments). Do you think he should try .dropna()?Mark Moretto
@AanAndriatno Try user.dtypes or user.info() Or, user["isorg"].dtypeMark Moretto

1 Answers

2
votes

try the following

for row in df[df['isorg']==0].itertuples():