Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Groupby和frequency count未返回正确的值_Python_Pandas - Fatal编程技术网

Python Groupby和frequency count未返回正确的值

Python Groupby和frequency count未返回正确的值,python,pandas,Python,Pandas,我正在使用此代码对公司进行分组和频率计数。然而,返回的结果并没有将这些公司分组 freq = df.groupby(['company'])['recruitment'].size() I got some result similar to this. recruitment company Data Co 3 Data Co 8 Apple Co 3 Apple Co 6 我有两个问题:

我正在使用此代码对公司进行分组和频率计数。然而,返回的结果并没有将这些公司分组

freq = df.groupby(['company'])['recruitment'].size()


I got some result similar to this. 
             recruitment
company      
  Data Co       3
  Data Co       8
  Apple Co      3
  Apple Co      6
我有两个问题:

  • 为什么这个groupby没有将相同的公司分组
  • 当我放置freq.columns时。它只显示招聘栏,公司消失了。是否有两个栏目同时显示公司和招聘

  • 如果公司名称看起来是“相同的”,那么前面或后面都有空格,我也在添加大写的convert all to大写

    freq = df.groupby(df['company'].str.strip().str.upper())['recruitment'].size()
    

    如果公司名称看起来是“相同的”,那么前面或后面都有空格,我也在添加大写的convert all to大写

    freq = df.groupby(df['company'].str.strip().str.upper())['recruitment'].size()
    

    如果看不到您的实际数据,就无法判断,但我猜您的公司名称可能会有细微差异,例如尾随空格或隐藏字符。这对我来说很好。。。请包含数据框中的一些示例数据,但groupby应该可以工作。关于你的第二个问题,也许你需要的是
    reset_index()
    你的freq-df,然后
    freq.columns
    。没有看到你的实际数据就无法判断,但我猜你的公司名称可能会有细微的差异,比如尾随空格或隐藏字符。这对我来说很好。。。请包含数据框中的一些示例数据,但groupby应该可以工作。关于第二个问题,可能您需要的是
    reset_index()
    freq-df,然后
    freq.columns