Home > OS >  groupby method with filter functions and conditions
groupby method with filter functions and conditions

Time:03-31

This the data set

f = pd.DataFrame({'Movie': ['name1','name2','name3','name4'],
                  'genre': ['sci-fci', 'action','comedy','action'],
                  'distributor': ['disney', 'disney','parmount','disney'],})

This is what , I did this but got an error

res=f.groupby(['Genre']).filter(["Distributor"]=='Walt Disney Studios Motion Pictures')

I want to group by genre and only have movies launched by disney eg want an output like a table of disney has 1 sci-fic 2 action

CodePudding user response:

This finds the rows with distributer equal to disney and groups the dataframe by genre

f = f[f['distributor'] == 'disney']
res = f.groupby(['genre'])

One line code could be

res = f[f['distributor'] == 'disney'].groupby(['genre'])

CodePudding user response:

f[f['distributor'] == 'disney'].groupby(['genre']).count()
  • Related