我的数据设置如下:
Year Month Dryden 3rdAve Clark Landfill
0 2015 1 0.00 0.00 0.0 NaN
1 2015 1 0.00 0.00 0.0 NaN
2 2015 1 0.00 0.00 0.0 NaN
3 2015 1 0.00 0.00 0.0 NaN
4 2015 1 0.00 0.00 0.0 NaN
5 2015 1 0.00 0.00 0.0 NaN
6 2015 1 0.00 0.00 0.0 NaN
7 2015 1 0.00 0.00 0.0 NaN
8 2015 1 0.00 0.00 0.0 NaN
9 2015 1 0.00 0.00 0.0 NaN
10 2015 1 0.00 0.00 0.0 NaN
11 2015 1 0.00 0.00 0.0 NaN
我要运行以下代码来计算Dryden值的每个季节的平均值:
df.Dryden.groupby([df.Year,pd.cut(df.Month,[0,3,6,9,12],labels=['Winter','Spring','Summer','Autumn'],right =False)]).mean()
我收到此错误:
TypeError:“>”和“ str”的实例之间不支持“>”
df.dtype给我:
Year int64
Month object
Dryden float64
3rdAve float64
Clark float64
Landfill float64
dtype: object
我想知道是否有人可以帮助我。
答案 0 :(得分:0)
将“月”列转换为这样的整数:
df.Month = df.Month.astype(int)
然后运行您的代码:
In [61]: df.Dryden.groupby([df.Year,pd.cut(df.Month,[0,3,6,9,12],labels=['Winter','Spring','Summer','Autumn'],right =False)]).mean()
Out[61]:
Year Month
2015 Winter 0.0
Name: Dryden, dtype: float64
如果您遇到值错误,则可以改用:
df.Month = pd.to_numeric(df.Month, errors='coerce')
答案 1 :(得分:0)
添加此内容:
df['Month'] = df['Month'].astype(int)
或者:
df['Month'] = df['Month'].astype(float).astype(int)