I want to group rows of a csv file, count in one column and add in the other.
For example with the following I would like to group the lines on the Commune to make columns of the winner with the count and a column Swing with the sum
Commune Winner Swing longitude latitude turnout
Paris PAM 1 12.323 12.093 0.3242
Paris PJD 0 12.323 12.093 0.1233
Paris PAM 1 12.323 12.093 0.534
Paris UDF 1 12.323 12.093 0.65434
Madrid PAM 0 10.435 -3.093 0.3423
Madrid PAM 1 10.435 -3.093 0.5234
Madrid PJD 0 10.435 -3.093 0.235
How to group rows, have a column in one column and a sum in the other?
Commune PAM PJD UDF Swing
Paris 3 1 1 3
Madrid 2 1 0 1
So far I tried try :
g = df.groupby('Commune').Winner
pd.concat([g.apply(list), g.count()], axis=1, keys=['members', 'number'])
But it returns:
members number
Commune
Paris [PAM, PJD, PAM, UDF] 4
Madrid [PAM, PAM, UDF] 3