jpen
jpen

Reputation: 1

Pandas dataframe multiple criteria

Following code is working

c1 = da['A']<=50 c2 = da['A']>=35 df = da[c1 & c2]['B']

But how can I write this in one line?

Upvotes: 0

Views: 34

Answers (1)

jezrael
jezrael

Reputation: 862441

Use DataFrame.loc for select by conditions and by column label:

df = da.loc[(da['A']<=50) & (da['A']>=35), 'B']

Your solution should be changed, but not recommended, because evaluation order matters - possible chained assignment with SettingWithCopyWarning, if assign value by mask (what is similar code like here):

df = da[(da['A']<=50) & (da['A']>=35)]['B']

Upvotes: 1

Related Questions