python - Use pandas groupby function on multiple columns -
i have dataframe similar this:
key departure species1 species2 status 1 r carlan carlan d 1 r scival carex c 2 r carlan scival d 2 r scival bougra c 3 d carlan carlan d 3 d scival scival c
i want count occurrences of each unique species1
given departure
, status
of d
of c
my desired output is:
species1 rd rc dd dc carlan 2 nan 1 nan scival nan 2 nan 1
use pandas.crosstab() method. single line of code:
pd.crosstab(df.species1, [df.departure, df.status])
the resulting table:
if combine @dermen's 'comb' column,
df['comb'] = df.departure + df.status pd.crosstab(df.species1, df.comb)
you'll get:
if really want 'nan', tack on .replace('0', np.nan)
, (assuming import numpy np
has been done):
pd.crosstab(df.species1, df.comb).replace('0', np.nan)
Comments
Post a Comment