简体   繁体   中英

Pandas groupby slice of a string

I have a dataframe where I want to group by the first part of an ID field. For example, say I have the following:

>>> import pandas as pd
>>> df=pd.DataFrame(data=[['AA',1],['AB',4],['AC',5],['BA',11],['BB',2],['CA',9]], columns=['ID','Value'])
>>> df
   ID  Value
0  AA      1
1  AB      4
2  AC      5
3  BA     11
4  BB      2
5  CA      9
>>> 

How can I group by the first letter of the ID field?

I can currently do this by creating a new column and then grouping on that, but I imagine there is a more efficient way:

>>> df['GID']=df['ID'].str[:1]
>>> df.groupby('GID')['Value'].sum()
GID
A    10
B    13
C     9
Name: Value, dtype: int64
>>> 

您将需要以某种方式创建分组键,而不必在DataFrame本身上创建,例如:

df.groupby(df.ID.str[:1])['Value'].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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM