我有超过33,000行的数据,我想简化如下:
Crime type
GeographyCode
E01006687 Burglary
E01007229 Anti-social behaviour
E01007229 Anti-social behaviour
E01007229 Anti-social behaviour
E01007229 Burglary
E01007229 Other theft
E01007229 Other theft
E01007229 Shoplifting
E01007229 Theft from the person
E01007230 Anti-social behaviour
E01007230 Anti-social behaviour
E01007230 Anti-social behaviour
E01007230 Anti-social behaviour
E01007230 Anti-social behaviour
E01007230 Anti-social behaviour
...“GeographyCode”有207个唯一值,“犯罪类型”有12个唯一值。
我想创建一个新的dataframe,它有207行和12列,加上“GeographyCode”索引列,每一列代表犯罪类型,并包含GeographyCode中发生的所有犯罪类型的计数。
就像这样:
Burglary Anti-social Theft Shoplifting etc...
GeographyCode
E01006687 1 3 9 5 ...
E01007229 1 3 2 1 ...
E01007230 0 6 12 5 ...
...我尝试过一些东西,但是由于没有数值,我发现很难得到我所需要的东西。
发布于 2015-11-16 10:09:43
您可以使用crosstab计算以下内容:
>>> pd.crosstab(df.index, df['Crime type'])
Crime type Anti-social behaviour Burglary Other theft Shoplifting ...
E01006687 0 1 0 0
E01007229 3 1 2 1
E01007230 6 0 0 0https://stackoverflow.com/questions/33732697
复制相似问题