【问题标题】:How to find the different variables of a column [duplicate]如何找到列的不同变量[重复]
【发布时间】:2020-06-08 11:36:51
【问题描述】:

数据集如下

no,store_id,revenue,profit,state,country,label
0,101,779183,281257,WD,India,1
1,101,144829,838451,WD,India,0
2,101,766465,757565,AL,Japan,-1
3,102,766465,757565,AL,Japan,-1
  • 不同的标签是-1,0,1
  • 不同的国家是India, Japan

  • 如何区分每个国家的计数也喜欢 India = 2, Japan = 2

【问题讨论】:

  • df['column'].value_counts()df['column'].unique()
  • @MayankPorwal 又添加了一条建议

标签: pandas


【解决方案1】:
df = df.groupby(by=['country']).size()
print(df)

country
India    2
Japan    2

【讨论】:

    【解决方案2】:

    您可以使用unique 方法来执行此操作。对于任何列,df.column.unique() 提供所有唯一值。对于计数,您可以使用df.column.value_counts()

    【讨论】:

    • 如何查看,如何获取每个国家的计数,如``日本 = 4, India =2
    • Japan=4 的计数如何?
    • df['country'].value_counts()
    猜你喜欢
    • 1970-01-01
    • 2015-07-14
    • 2016-01-21
    • 1970-01-01
    • 2021-02-12
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多