简体   繁体   English

在这种情况下,如何获取列并从 Pandas 数据透视表中获取计数?

[英]how to fetch columns and take the count from pandas pivot table in this case?

Let's say there is Pandas pivot table假设有 Pandas 数据透视表

country     AFG AHO ALG ANZ ARG     
edition
1800        2    1  1    1   1
1881        3    3  1    3   nan
1882        4    2  4    4   nan
1883        1    1  5    5   nan
1884        3    4  6    1   1
1885        4    5  7    2   3
1886        5   nan 1    2   5
1887       nan  nan 3    6   1

After creating a pivot table from a df I am trying to find how many countries have won at least one medal in each edition.从 df 创建数据透视表后,我试图找出每个版本中至少获得一枚奖牌的国家/地区。

can anyone tell me how to fetch the count of the country from pivot table?谁能告诉我如何从数据透视表中获取国家/地区的计数? in this case the answer should be 2 (ALG,ANZ)在这种情况下,答案应该是 2 (ALG,ANZ)

You can do:你可以做:

s = df_pivot.ge(1).all().sum()
# s = 2

To get the country names, you can do要获取国家/地区名称,您可以执行以下操作

s = df_pivot.ge(1).all()
s.index[s]
# output: Index(['ALG', 'ANZ'], dtype='object')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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