[英]Python pandas: accumulate data frame rows by condition
I have a data frame with 2 columns in following format: 我有一个2列的数据框,格式如下:
Anna 15
Mary 14
Elizabeth 11
Margaret 10
Alice 6
Bertha 5
Helen 5
Emily 4
Maria 4
Marie 4
Catherine 4
Marion 4
Ellen 4
Florence 4
Augusta 4
...
Juliette 1
Mara 1
Elise 1
Alfrida 1
Nourelain 1
Margaretta 1
Manca 1
Aloisia 1
Hulda 1
Clear 1
Wendla 1
Ellis 1
Lulu 1
Juliet 1
Gertrude 1
How can I accumulate rows with value < 5
to get something like 如何累积
value < 5
行以获得类似
Anna 15 Mary 14 Elizabeth 11 Margaret 10 Alice 6 Bertha 5 Helen 5 Other 50 安娜15玛丽14伊丽莎白11玛格丽特10爱丽丝6伯莎5海伦5其他50
here is a way: 这是一种方法:
# create some random data
df =pd.DataFrame({'letter': list('qwertyuiopasdfghjklzxcvbnm'),'value': np.random.randint(1,15,26)})
define a function to replace letters where value < 5 with other: 定义一个函数用其他值替换值<5的字母:
def f(x):
if x.value <5:
l= 'other'
else:
l =x.letter
return l
apply the function to the dataframe: 将函数应用于数据框:
df['letter'] =df.apply(f,axis=1)
group by the new letter column and sum: 按新字母列和总和分组:
df.groupby('letter').sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.