Search code examples
pythonpandasdataframepandas-groupbypandas-loc

store values after groupby and loc


using df to select two columns col_1 and col_2 after groupby and loc and store at variable a in dataframe format

Python Code

import pandas as pd
import numpy as np

df = pd.DataFrame(dict(Gender=['M', 'F', 'M', 'F',
                           'F', 'M', 'M', np.nan],
                   State=['NY','IL', 'NY', 'CA',
                          'IL', 'CA', 'CA', 'IL'],
                   col_1=[10,20,30,np.nan,40,50,60,70],
                   col_2=[1,6,2,4,6,9,np.nan,3]))

d = df.groupby(['Gender','State']).sum()
d.loc['F']

Execution Result

       col_1    col_2
State       
CA     0.0      4.0
IL     60.0     12.0

Expected Result (dataframe format)

  a = 
           col_1    col_2
   
           0.0      4.0
           60.0     12.0

Solution

  • You can reset the index after obtaining grupby data d.

    print(d.loc['F'].reset_index(drop=True))
    
       col_1  col_2
    0    0.0    4.0
    1   60.0   12.0