Reputation: 1772
I am new to python :)
I have the following dataframe, some columns are without header:
I am trying to do pretty basic condition for each name: like if dinner print "Good Evening".
What I did so far is trying to find the last non NAN values for each row:
df.ffill(axis=1).iloc[:, -1]
Result:
0 dinner
1 lunch
2 dinner
3 lunch
4 breakfast
5 lunch
6 lunch
7 dinner
Name: Unnamed: 11, dtype: object
Then trying to carry out the condition using for:
for index, column in df.iterrows():
if df.ffill(axis=1).iloc[:, -1] == "dinner":
print ("Good evening",column['Name'])
I got this error:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I then tried:
for i in df.ffill(axis=1).iloc[:, -1]:
if i == "dinner":
print ("Good evening",column['Name'])
But the condition does not work as expected!
Good evening Sara
Good evening Sara
Good evening Sara
Thanks for help
Upvotes: 0
Views: 171
Reputation: 35626
last_valid_index is also an option:
m = df.apply(lambda s: s[s.last_valid_index()], axis=1).eq('dinner')
for name in df.loc[m, 'Name']:
print('Good evening', name)
Good evening Sara
Good evening Remon
Good evening Alber
Or create a new series:
m = df.apply(lambda s: s[s.last_valid_index()], axis=1).eq('dinner')
print('Good evening ' + df.loc[m, 'Name'])
0 Good evening Sara
2 Good evening Remon
3 Good evening Alber
Name: Name, dtype: object
Upvotes: 1