Sum Grouped Pandas Dataframe By Single Column
I have a Pandas dataframe: test=pd.DataFrame(columns=['GroupID','Sample','SampleMeta','Value']) test.loc[0,:]='1','S1','S1_meta',1 test.loc[1,:]='1','S1','S1_meta',1 test.loc[2,:]=
Solution 1:
Well, you can include SampleMeta
as part of the groupby:
print test.groupby(['GroupID','Sample','SampleMeta']).sum()
Value
GroupID Sample SampleMeta
1 S1 S1_meta 2
2 S2 S2_meta 1
If you don't want SampleMeta
as part of the index when done you could modify it as follows:
print test.groupby(['GroupID','Sample','SampleMeta']).sum().reset_index(level=2)
SampleMeta Value
GroupID Sample
1 S1 S1_meta 2
2 S2 S2_meta 1
This will only work right if there is no variation within SampleMeta
for ['GroupID','Sample']
. Of course, If there was variation within ['GroupID','Sample']
then you probably to exclude SampleMeta
from the groupby/sum entirely:
print test.groupby(['GroupID','Sample'])['Value'].sum()
GroupID Sample
1 S1 2
2 S2 1
Post a Comment for "Sum Grouped Pandas Dataframe By Single Column"