pandas重新采样命令继续运行



我的DataFrame看起来像

trip_day_df.head
Out[18]: 
<bound method NDFrame.head of              
INSERTED_UTC        VALUE
0 2017-11-03 10:30:31.430    981
1 2017-09-25 22:15:26.757   2787
2 2017-12-17 23:49:24.880   2591
3 2019-02-04 23:07:30.083  45544
4 2019-01-12 11:35:32.657    504>

我想按行对INSERTED_UTC进行分组,并求和"VALUE"。期望输出

INSERTED_UTC    VALUE
2017-12-31      6359
2018-12-31      0
2019-12-31      46048
trip_day_df.dtypes
Out[11]: 
INSERTED_UTC    datetime64[ns]
VALUE                   object
trip_day_df.iloc[0,1]
Out[12]: '981'
print(type(trip_day_df.iloc[0,1]))
<class 'str'>

当我运行该命令时,要按年份对INSERTED_UTC进行分组,并将count的值相加,该命令将继续运行。

df_year = trip_day_df.resample('Y', on='INSERTED_UTC').sum()

数据最初有超过一百万行,当我运行5行的小数据时,它会给出一个奇怪的输出。它只是将VALUE排列在一起,而不是将其相加

INSERTED_UTC    VALUE
2017-12-31  27879812591
2018-12-31  0
2019-12-31  50445544

我觉得问题出在'VALUE'列是字符串

print(type(trip_day_df.iloc[0,1]))
<class 'str'>

我把它的数据类型改为浮动

trip_day_df['VALUE'] = pd.to_numeric(trip_day_df['VALUE'])

更改的数据类型,

trip_day_df.dtypes
Out[44]: 
INSERTED_UTC    datetime64[ns]
VALUE                    int64
dtype: object

现在,

trip_day_df.resample('Y', on='INSERTED_UTC').sum()
Out[47]: 
VALUE
INSERTED_UTC       
2017-12-31     6359
2018-12-31        0
2019-12-31    46048

最新更新