Aan Andriatno
Aan Andriatno

Reputation: 11

Looping itertuples() in python with row condition

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.

Upvotes: 0

Views: 1308

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113988

try the following

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

Upvotes: 2

Related Questions