hartmut
hartmut

Reputation: 982

pd.groupby by column + list

I know how to use .groupby over more than one column, namely:

df.groupby(['col5', 'col2']).size() 

I also know how to use .groupby using a list that is distinct/separate from the dataframe I am working with, namely:

df.groupby(aList).size()    

But how can I use .groupby using, simultaneously, a separate list and one (or some) of the columns of the dataframe I am working with. I.e, what is the correct way to write:

df.groupby(aList, aColumnName).size()    

? Thanks.

Upvotes: 1

Views: 66

Answers (1)

jezrael
jezrael

Reputation: 862611

You can join lists together with +:

#for list with one column name
df.groupby(aList + [aColumnName]).size()  
#for join 2 lists
df.groupby(aList + collist2).size()  
#for join with columns
df.groupby(aList + df.columns[:2].tolist()).size()  

Upvotes: 2

Related Questions