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:

enter image description here

if combine @dermen's 'comb' column,

df['comb'] = df.departure + df.status pd.crosstab(df.species1, df.comb) 

you'll get:

enter image description here

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) 

enter image description here


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -