lili.b
lili.b

Reputation: 81

What is the Pythonic way to pull conditional cell value from pandas dataframe

My data frame, new_df, includes 3 columns: "name", "gender", and "Total". I need to pull the value from the total column per a combination of name and gender.

        name        gender  Total
357328  Barbara     F       3114462
357329  Patricia    F       3846693
357330  Betty       F       1138056
357331  Shirley     F       1075104
357332  Carol       F       2054088
my_name = 'Betty'
new_df[(new_df.name == my_name) & (new_df.gender == 'F')]['Total'])

This code works returns the correct value, 1138056, but I am sure there is a better way to get the result.

Upvotes: 0

Views: 72

Answers (2)

Mark Wang
Mark Wang

Reputation: 2757

Personally, I prefer using loc with lambda function as it does not require creating the dataframe in the first place and can be used as part of a method chain.

new_df.loc[lambda x: (x.name == my_name) & (x.gender == 'F'), 'Total']

Upvotes: 0

jezrael
jezrael

Reputation: 862406

You can combine boolean indexing with DataFrame.loc for filter by mask and also by column name, output is Series:

my_name = 'Betty'
s = new_df.loc[(new_df.name == my_name) & (new_df.gender == 'F'), 'Total']

print (s)
357330    1138056
Name: Total, dtype: int64

And then if matched at least one value and need select first:

print (s.values[0])
1138056
print (s.iloc[0])
1138056
print (s.iat[0])
1138056

Or if possible no match value is possible use next with iter, where is possible set default value:

print (next(iter(s), 'no match'))
1138056

my_name = 'John'
s = new_df.loc[(new_df.name == my_name) & (new_df.gender == 'F'), 'Total']

print (next(iter(s), 'no match'))
no match

Upvotes: 1

Related Questions