python - 如何将 groupby 函数与 count() 一起使用,条件仅在 i>Num 时计数

标签 python pandas pandas-groupby

print (df)
     Model  Brand   Type  Jan  Feb  Mar  April  May
0     C310   Benz    Car   10    8   13     29    0
1     C320   Benz    Car   15   11   16      0    7
2    Focus   Ford    Car   10    0    5     20    8
3     F150  Focus  Truck    5    0    5      0    2
4  Ram1500  Dodge  Truck    2    2    7      0    1
5  Caravan  Dodge    Van   20    8   13      5    0
6  Charger  Dodge    Car   17    0    5     10   15

我想将数据按两列分组,并计算其他列中的值是否大于 Num,其中 Num 可以是任何整数值。

例如,

df.groupby(['Type','Brand']).count() 

但我只想在该值大于 Num 时才进行计数。

非常感谢您的帮助。预先感谢您。

最佳答案

使用numpy.where将值替换为 NaN,因为 GroupBy.count排除它们:

Num = 10
cols = df.columns.difference(['Type','Brand', 'Model'])
df[cols] = np.where(df[cols] > Num, df[cols], np.nan)
df = df.groupby(['Type','Brand'])[cols].count() 

print (df)
             April  Feb  Jan  Mar  May
Type  Brand                           
Car   Benz       1    1    1    2    0
      Dodge      0    0    1    0    1
      Ford       1    0    0    0    0
Truck Dodge      0    0    0    0    0
      Focus      0    0    0    0    0
Van   Dodge      0    0    1    1    0

关于python - 如何将 groupby 函数与 count() 一起使用,条件仅在 i>Num 时计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53425103/

相关文章:

Python将一个long int作为二进制值写入文件

Python - 正则表达式查找所有重复模式,后跟可变长度的字符

python - 脚本无法从网页获取数据

python - 用pandas在csv中上传csv

python - pandas dataframe merge 是否可以更大或更小地工作?

python - 其他专栏的 Pandas filldown(我不知道如何命名)

python-3.x - AttributeError : Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, 尝试使用 'apply' 方法

python - 将 src 文件夹中的文件导入到 test 文件夹中

csv - 使用 MultiIndex 读取 CSV 文件

python - 使用 pandas GroupBy 聚合字符串列