![](/img/trans.png)
[英]pandas dataframe: resample to include all the dates from today's date
[英]Pandas resample function adds dates that were not in the Dataframe
我有一個包含大量數據的 2019 年 1 月 Dataframe。
在照片中,數據 - 僅 1 月 - 由 Ciclo_Estacion_Retiro == 271 分組 -using.loc 函數
series=read_csv('2019-01.csv',header=0,parse_dates=True,squeeze=True)
series=series.dropna(how='all')
print(series.head())
Out[]:
Genero_Usuario Edad_Usuario Bici Ciclo_Estacion_Retiro Fecha_Retiro \
0 M 36 11444 67 01/01/19
1 M 36 9196 55 01/01/19
2 M 38 2612 29 01/01/19
3 M 43 7463 29 01/01/19
4 M 37 3987 136 01/01/19
Hora_Retiro Ciclo_Estacion_Arribo Fecha_Arribo Hora_Arribo
0 0:08:44 36 01/01/19 0:14:03
1 0:12:05 136 01/01/19 0:42:11
2 0:13:15 115 01/01/19 0:37:47
3 0:13:36 115 01/01/19 0:38:04
4 0:15:30 35 01/01/19 0:22:47
In[]:
series['fecha_solicitud']=series[['Fecha_Retiro','Hora_Retiro']].agg(' '.join,axis=1)
series['fecha_entrega']=series[['Fecha_Arribo','Hora_Arribo']].agg(' '.join,axis=1)
series['fecha_solicitud']=pandas.to_datetime(series['fecha_solicitud'],infer_datetime_format=True)
series['fecha_entrega']=pandas.to_datetime(series['fecha_entrega'],infer_datetime_format=True)
series=series.drop('Fecha_Arribo',axis=1)
series=series.drop('Hora_Arribo',axis=1)
series=series.drop('Fecha_Retiro',axis=1)
series=series.drop('Hora_Retiro',axis=1)
first_column = series.pop('fecha_entrega')
series.insert(0, 'fecha_entrega', first_column)
first_column = series.pop('fecha_solicitud')
series.insert(0, 'fecha_solicitud', first_column)
print(series.head())
Out[]:
fecha_solicitud fecha_entrega Genero_Usuario Edad_Usuario Bici \
0 2019-01-01 00:08:44 2019-01-01 00:14:03 M 36 11444
1 2019-01-01 00:12:05 2019-01-01 00:42:11 M 36 9196
2 2019-01-01 00:13:15 2019-01-01 00:37:47 M 38 2612
3 2019-01-01 00:13:36 2019-01-01 00:38:04 M 43 7463
4 2019-01-01 00:15:30 2019-01-01 00:22:47 M 37 3987
Ciclo_Estacion_Retiro Ciclo_Estacion_Arribo
0 67 36
1 55 136
2 29 115
3 29 115
4 136 35
In[]:
repetidos=series['Ciclo_Estacion_Retiro'].value_counts()
repetidos10=DataFrame(repetidos.head(10))
repetidos10
Out[]:
Ciclo_Estacion_Retiro
271 7442
27 6533
1 6370
182 5044
36 4493
64 4453
43 4402
18 4363
47 4299
266 4221
In[]:
repetidos10=repetidos10.reset_index()
repetidos10.columns=['Estacion','Frecuencia']
repetidos10
Out[]:
Estacion Frecuencia
0 271 7442
1 27 6533
2 1 6370
3 182 5044
4 36 4493
5 64 4453
6 43 4402
7 18 4363
8 47 4299
9 266 4221
repetidos10.Estacion[0]=271 # repetidos10 是DataFrame 需求量最高的10個站
In[]:
origen_Test=pandas.DataFrame(series.loc[(series['Ciclo_Estacion_Retiro'] ==repetidos10.Estacion[0])])
origen_Test
Out[]:
fecha_solicitud fecha_entrega Genero_Usuario Edad_Usuario Bici Ciclo_Estacion_Retiro Ciclo_Estacion_Arribo
107 2019-01-01 07:31:34 2019-01-01 07:38:09 M 53 8064 271 26
117 2019-01-01 07:42:57 2019-01-01 07:47:05 M 63 7825 271 270
124 2019-01-01 07:45:03 2019-01-01 07:55:09 M 44 3936 271 107
137 2019-01-01 07:57:03 2019-01-01 08:01:21 M 39 10213 271 10
157 2019-01-01 08:14:09 2019-01-01 08:26:28 M 41 10734 271 46
... ... ... ... ... ... ... ...
695937 2019-01-31 21:29:27 2019-01-31 21:40:18 M 49 1849 271 26
696011 2019-01-31 21:33:05 2019-01-31 21:57:46 F 29 12373 271 74
696467 2019-01-31 22:05:15 2019-01-31 22:13:31 M 38 9491 271 113
696929 2019-01-31 22:52:38 2019-01-31 23:02:47 M 28 7914 271 174
696957 2019-01-31 22:57:00 2019-01-31 23:25:39 M 37 10974 271 271
7442 rows × 7 columns
我想每 30 分鍾使用 Pandas 重新采樣 function 對它們進行一次分組,但是這樣做會增加我沒有的月份,也就是說,它會更改日期......
origen_30min_Test=origen_Test.resample('30min',on='fecha_solicitud').Ciclo_Estacion_Arribo.count()
print(origen_30min_Test)
fecha_solicitud
2019-01-01 07:30:00 4
2019-01-01 08:00:00 2
2019-01-01 08:30:00 0
2019-01-01 09:00:00 0
2019-01-01 09:30:00 1
..
2019-12-01 21:00:00 1
2019-12-01 21:30:00 1
2019-12-01 22:00:00 0
2019-12-01 22:30:00 0
2019-12-01 23:00:00 2
Freq: 30T, Name: Ciclo_Estacion_Arribo, Length: 16064, dtype: int64
第二張照片是執行重采樣 function 30min 的結果
第二張照片是執行重采樣 function 30min 的結果
我已經嘗試了幾件事......但它們對我不起作用,你能幫幫我嗎
I plot 開頭的原始數據,僅出現 2019 年 1 月的數據。 沒關系
在此行之后使用重采樣 function 后出現“錯誤”...
series['fecha_solicitud']=pandas.to_datetime(series['fecha_solicitud'],infer_datetime_format=True)
我不得不使用infer_datetime_format=True因為原始 Hour 數據只有一位數字,即:
01/01/19 0:14:03
我無法將其更改為(拋出錯誤)
01/01/19 00:14:03
你知道我如何將 0 添加到時間的 HOUR 數據中,我想用這個我可以解決它。 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.