1

如何使用 python pandas 在由几列分组的列中找到元素的计数?

我有以下 csv 文件结构:

'Country'     'City'     'Year'  'Month'   'Value'    'Street_Code'
 USA          New York    1971     jan        0.0        1
 USA          New York    1971     feb       23.5        1
 USA          New York    1971     mar       10.2        1
 USA          Florida     1971     jan        0.0        1
 USA          Florida     1971     feb        0.0        1
 USA          Florida     1971     mar        0.0        1
 USA          New York    1971     jan        0.0        2
 USA          New York    1971     feb        15.0       2
 USA          New York    1971     mar        7.6        2
 USA          Florida     1971     jan        0.0        2
 USA          Florida     1971     feb        0.0        2
 USA          Florida     1971     mar        2.3        2

我想'value'通过分组来计算零(0.0)的数量'Country', 'City', 'Year'& 'Street Code'

到目前为止,我已经尝试过了;

import pandas as pd
data = pd.read_csv('country_details.csv')
count_data = data[data['Value'] == 0.0] # I'm filtering the data. I don't think this is the right way of doing it
grouped = count_data.groupby(['Country','Year','Month','Street_Code']) # I'm stuck here
4

1 回答 1

2

您的过滤几乎是正确的,但您需要引用列名,在本例中为“值”。

尝试:

import pandas as pd
import StringIO

csv = StringIO.StringIO("""Country,City,Year,Month,Value,Street_Code
USA,NewYork,1971,jan,0.0,1
USA,NewYork,1971,feb,23.5,1
USA,NewYork,1971,mar,10.2,1
USA,Florida,1971,jan,0.0,1
USA,Florida,1971,feb,0.0,1
USA,Florida,1971,mar,0.0,1
USA,NewYork,1971,jan,0.0,2
USA,NewYork,1971,feb,15.0,2
USA,NewYork,1971,mar,7.6,2
USA,Florida,1971,jan,0.0,2
USA,Florida,1971,feb,0.0,2
USA,Florida,1971,mar,2.3,2""")

data = pd.read_csv(csv)

datasub = data[data['Value'] == 0.0]

print datasub.groupby(['Country','Year','Month','Street_Code'])['Value'].count()

Country  Year  Month  Street_Code
USA      1971  feb    1              1
                      2              1
               jan    1              2
                      2              2
               mar    1              1
于 2013-04-23T14:19:33.670 回答