所以,我遇到了一个有趣的条形图。
我发现了
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
我才刚刚开始使用熊猫。我明天再试一次,如果有人知道的话…