代码之家  ›  专栏  ›  技术社区  ›  S Meaden

pandas-如何按括号和唯一列值分组?

  •  0
  • S Meaden  · 技术社区  · 6 年前

    所以,我遇到了一个有趣的条形图。 enter image description here 我发现了 underlying data here 我正在尝试重新创建数据是如何按范围箱分组的(我使用了 pd.cut )按国家划分。

    这是我到目前为止尝试过的代码,但是我得到了错误,(errorneous)行被注释掉了

    import pandas as pd
    
    ## csv file in zip http://ec.europa.eu/eurostat/cache/GISCO/geodatafiles/GEOSTAT-grid-POP-1K-2011-V2-0-1.zip
    
    url="C:/Users/Simon/Downloads/GEOSTAT-grid-POP-1K-2011-V2-0-1/Version 2_0_1/GEOSTAT_grid_POP_1K_2011_V2_0_1.csv"
    whole=pd.read_csv(url, low_memory=False)
    
    populationDensity=whole[['TOT_P','CNTR_CODE']]
    
    
    ## trying to replicate graph here http://www.centreforcities.org/wp-content/uploads/2018/04/18-04-16-Square-kilometre-units-of-land-by-population.png
    ## which aggregates the records by brackets
    
    
    # https://stackoverflow.com/questions/25010215/pandas-groupby-how-to-compute-counts-in-ranges#answer-25010952
    ranges = [0,10000,15000,20000,25000,30000,35000,40000,45000,1000000]
    bins=pd.cut(populationDensity['TOT_P'],ranges)
    
    
    
    #print(bins)
    
    ## the following fails with error :
    ## AttributeError: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects, try using the 'apply' method
    #print (populationDensity.groupby(['CNTR_CODE']).groupby(bins).count())
    
    ## the following fails with error :
    ## TypeError: 'Series' objects are mutable, thus they cannot be hashed
    print (populationDensity.groupby(['CNTR_CODE'],pd.cut(populationDensity['TOT_P'],ranges)).count())
    
    #relevant https://stackoverflow.com/questions/21441259/pandas-groupby-range-of-values#answer-21441621
    

    我才刚刚开始使用熊猫。我明天再试一次,如果有人知道的话…

    1 回复  |  直到 6 年前
        1
  •  1
  •   jezrael    6 年前

    更改:

    print (populationDensity.groupby(['CNTR_CODE'],pd.cut(populationDensity['TOT_P'],ranges)).count())
    

    print (populationDensity.groupby(['CNTR_CODE', pd.cut(populationDensity['TOT_P'],ranges)]).count())
                                                ^                                           ^
    

    因为 groupby 参数 by 在中使用多个列名、组合列名和序列或多个序列 list :

    :映射、函数、标签或 标签列表

    用于确定GroupBy的组。如果by是一个函数,则对对象索引的每个值调用它。如果传递了dict或series,则将使用series或dict值来确定组(序列值首先对齐;请参见.align()方法)。如果传递了ndarray,则按原样使用值来确定组。标签或标签列表可以通过自身的列传递给组。注意,元组被解释为(单个)键。