[英]how to count positive and negative numbers of a column after applying groupby in pandas
have the following dataframe:有以下 dataframe:
token name ltp change
0 12345.0 abc 2.0 NaN
1 12345.0 abc 5.0 1.500000
2 12345.0 abc 3.0 -0.400000
3 12345.0 abc 9.0 2.000000
4 12345.0 abc 5.0 -0.444444
5 12345.0 abc 16.0 2.200000
6 6789.0 xyz 1.0 NaN
7 6789.0 xyz 5.0 4.000000
8 6789.0 xyz 3.0 -0.400000
9 6789.0 xyz 13.0 3.333333
10 6789.0 xyz 9.0 -0.307692
11 6789.0 xyz 20.0 1.222222
I need to count of positive and negative number for each category of the name column.我需要计算名称列的每个类别的正数和负数。 in above example
在上面的例子中
abc:pos_count: 3 abc:neg_count:2
xyz:pos_count:2 xyz:neg_count:2
count=df.groupby('name')['change'].count()
count
however, this gives me only the total count by group but not the positive & negative count separately.但是,这仅给我按组的总计数,而不是分别给出的正负计数。
Use:利用:
g = df.groupby('name')['change']
counts = g.agg(
pos_count=lambda s: s.gt(0).sum(),
neg_count=lambda s: s.lt(0).sum(),
net_count=lambda s: s.gt(0).sum()- s.lt(0).sum()).astype(int)
Result:结果:
# print(counts)
pos_count neg_count net_count
name
abc 3 2 1
xyz 3 2 1
Use np.sign
with Series.map
for new column added by DataFrame.assign
and then count values by SeriesGroupBy.value_counts
:将
np.sign
与Series.map
用于SeriesGroupBy.value_counts
DataFrame.assign
值:
count=(df.assign(type=np.sign(df['change'])
.map({1:'pos_count', -1:'neg_count'}))
.groupby(df['name'])['type']
.value_counts()
.reset_index(name='count'))
print (count)
name type count
0 abc pos_count 3
1 abc neg_count 2
2 xyz pos_count 3
3 xyz neg_count 2
You can create a new column in df
with the sign of change
and group by name
and sign:您可以在
df
中创建一个带有change
符号的新列,并按name
和符号分组:
import pandas as pd
import numpy as np
df['change_sign'] = np.sign(df['change'])
df.groupby(['name','change_sign']).count()
You can then pivot if you need the result in columns instead of rows如果您需要列而不是行中的结果,您可以然后 pivot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.