Reputation: 982
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
Reputation: 862611
You can join list
s 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