I've got a single-column dataframe with an index of integers represented as strings that has repeated values in it. The values are integers and I would like to have a dataframe with an index with no repeats in it and whose values are the sum of all the values that originally had the given index label. Here's a sample of the data I'm working with:
>>> verts
3 54
3 34
0 33
4 28
4 23
2 22
2 15
5 15
5 15
0 9
1 2
6 1
1 1
6 1
I could do it this way, but it doesn't seem like good pandas syntax:
new_index = set(verts.index)
new_vals = [verts[x].sum() for x in new_index]
new_df = pd.DataFrame({'Counts': new_vals}, index=new_index)
new_df
Counts
1 3
0 42
3 88
2 37
5 30
4 51
6 2
Is there something more straight-forward? Thanks.
Try resetting your index and then using groupby
:
verts = pd.Series([54, 34, 33, 28, 23, 22, 15, 15, 15, 9, 2, 1, 1, 1],
index=["3", "3", "0", "4", "4", "2", "2", "5", "5", "0", "1", "6", "1", "6"])
>>> verts.reset_index().groupby('index').sum()
0
index
0 42
1 3
2 37
3 88
4 51
5 30
6 2
Or specify level=0
to group on the index.
verts.groupby(level=0).sum()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.