RDs
RDs

Reputation: 521

Filter dataframe by sum

import pandas as pd 
df = pd.DataFrame({'Name': ['dd','ee','ff'], 'Toy-Figure': [2,3,1], 'Toy-car':[1,2,0], 'Toy-blocks':[5,9,0]})

For the above df, I want all Names who have more than 1 toy. Here's what I have so far, but I can't get the names column. How do I do that ?

df_c = df.copy()
df_c[df_c.sum(axis = 1) > 1].sum(axis=1)

Upvotes: 1

Views: 50

Answers (2)

Akash Karnatak
Akash Karnatak

Reputation: 738

I'll simply add to your solution,

df_c = df.copy()
df_c.index = df_c['Name']
df_c[df_c.sum(axis = 1) > 1].sum(axis=1)

or

df.sum(axis = 1)[df.sum(axis = 1) >1]

Upvotes: 1

Code Pope
Code Pope

Reputation: 5459

One way:

df[df['Toy-Figure']>1].Name

Result:

0    dd
1    ee

Upvotes: 1

Related Questions