【问题标题】:Pandas - I have a dataset where the clmns r country, company and total employees. I need a dataframe for total employees in each company by countryPandas - 我有一个数据集,其中包含国家、公司和员工总数。我需要按国家/地区划分每个公司的员工总数的数据框
【发布时间】:2021-07-15 23:23:52
【问题描述】:

总共有 8 家公司和大约 30 - 40 个国家。我需要获取一个数据框,我可以在其中了解每个国家/地区每个公司的员工总数。

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    听起来您想使用 Panda 的 groupby 功能。我不确定你有什么类型的数据以及你想要什么结果,所以这里有一些玩具示例:

    df = pd.DataFrame({'company': ["A", "A", "B"], 'country': ["USA", "USA", "USA"], 'employees': [10, 20, 50]})
    dfg = df.groupby(['company', 'country'], as_index=False)['employees'].sum()
    
    print(dfg)
    #   company country  employees
    # 0       A     USA         30
    # 1       B     USA         50
    
    df = pd.DataFrame({'company': ["A", "A", "A"], 'country': ["USA", "USA", "Japan"], 'employees': ['Art', 'Bob', 'Chris']})
    dfg = df.groupby(['company', 'country'], as_index=False)['employees'].count()
    
    print(dfg)
    #   company country  employees
    # 0       A   Japan          1
    # 1       A     USA          2
    

    【讨论】:

    • 我如何获取每个国家/地区的员工百分比?
    • 如果您接受我的回答并让我知道您是否愿意,我可以编写代码。您是否想要:对于每家公司,对于公司拥有员工的每个国家/地区,该公司员工在每个国家/地区的百分比是多少?
    • 我确实接受了你的回答。谢谢 !其实我需要另一个帮助。如果您可以帮助我处理另一个数据集,我将发布另一个问题。再次感谢
    • @Akira,谢谢,我很乐意看看你的下一个问题
    • 你能看看我的下一个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 2013-05-18
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多