如何合并日期上的dataframe列表以制作条形图?PandasPython

2024-10-06 12:17:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个数据帧列表,并希望将它们在日期(X轴)与其他列(Y轴)合并成条形图:

[        Date  Unavailability_mec_by_Month
0 2019-10-01                      2.235304
1 2019-11-01                      1.134355
2 2019-12-01                     19.392279
3 2020-01-01                     10.063748
4 2020-02-01                      0.731397,         Date  Unavailability_transport_by_Month
0 2019-12-01                            0.674824,         Date  Unavailability_stock_by_Month
0 2019-10-01                            0.228069
1 2019-11-01                            0.145258
2 2019-12-01                            0.202965
3 2020-01-01                            1.357004
4 2020-02-01                            0.550151,         Date  Unavailability_chain_by_Month
0 2019-11-01                            0.009746
1 2019-12-01                            0.003303
2 2020-01-01                            0.016242
3 2020-02-01                            0.003328,         Date  Unavailability_prod_by_Month
0 2019-10-01                                   14.620000
1 2019-11-01                                    0.447353
2 2019-12-01                                    4.822858
3 2020-01-01                                    5.565766
4 2020-02-01                                    0.435972

我试过这些,但不起作用:

from functools import reduce
#df_graph = reduce(lambda x, y: pd.merge(x, y, on = 'Date'), list_df_graph)
#reduce(lambda left,right: pd.merge(left,right,on='Date'), list_df_graph)
#merge=pd.merge(list_df_graph, how='outer', left_index=True, right_index=False)
df_graph = pd.concat(list_df_graph, axis=1)

和concat方法,给我一个结果,但不是预期的

谢谢你的时间

编辑 预期结果:

Date        Unavailability_mec_by_Month   Unavailability_transport_by_Month   ...
2019-10-01                     2.235304                            0.674824 
2019-11-01                     1.134355                                 NaN
2019-12-01                    19.392279                                 NaN
2020-01-01                    10.063748                                 NaN
2020-02-01                     0.731397                                 NaN

但目前,我有一个列日期用于每个不可用性,如下所示:

Date              mec_by_Month         Date     transport_by_Month         Date  ...
2019-10-01            2.235304   2019-10-01               0.674824   2019-10-01
2019-11-01            1.134355          Nan                    NaN          NaN
2019-12-01           19.392279          Nan                    NaN          NaN
2020-01-01           10.063748          Nan                    NaN          NaN
2020-02-01            0.731397          Nan                    NaN          NaN

Tags: reducedfdatebymergenanleftlist
1条回答
网友
1楼 · 发布于 2024-10-06 12:17:08

将列Date转换为索引,然后在列表理解中^{}Dates对齐值(转换为DatetimeIndex):

df_graph = pd.concat([x.set_index('Date') for x in list_df_graph], axis=1)

或要索引的第一列:

df_graph = pd.concat([x.set_index(x.columns[0]) for x in list_df_graph], axis=1)

相关问题 更多 >