1

I am doing some EDA on my data which is pandas data-frame and I am stuck at this point. I don't know how to count values of infants where party== republican or democrat. I want to apply value_counts on all columns where arty== republican or democrat.

   party infants water budget physician salvador religious satellite 
republican  n     y      n         y        y         y         n   
republican  n     y      n         y        y         y         n   
democrat    ?     y      y         ?        y         y         n   
democrat    n     y      y         n        ?         y         n   
democrat    y     y      y         n        y         y         n 

Answer should be like

party           infants 
democrat.    y=1, n=1, ?=1
republican   y=0, n=2, ?=0

like that for all columns. I have tried something like that.

infants_count = data["infants"].value_counts().where(data['party']=="democrat")
infants_count.loc['y']
halfer
  • 19,824
  • 17
  • 99
  • 186
shahid hamdam
  • 751
  • 1
  • 10
  • 24

1 Answers1

2

Let's try:

df_out = df.groupby('party')['infants'].value_counts().unstack(0).fillna(0)

df_out.apply(lambda x: ','.join(x.index +'='+x.astype(int).astype(str)))\
      .rename('infant')\
      .reset_index()

Output:

        party       infant
0    democrat  ?=1,n=1,y=1
1  republican  ?=0,n=2,y=0

Update to answer comment below:

df_out = df.groupby('party')['infants'].value_counts().unstack(-1).fillna(0)

df_out

Output:

infants       ?    n    y
party                    
democrat    1.0  1.0  1.0
republican  0.0  2.0  0

Plotting

df_out = df.groupby('party')['infants'].value_counts().unstack(-1).fillna(0)
ax = df_out.plot.bar(stacked=True)

Output:

enter image description here

Select a single value:

df_out.loc['democrat','y']

Output:

1.0
Scott Boston
  • 147,308
  • 15
  • 139
  • 187