Pandas/Python均衡不同的索引

2024-04-28 21:48:29 发布

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

我有两个数据帧

df1型:

                     0            1            2
Date                                                                          
2005-10-01   772.142457  5600.491978 -5522.102692  
2005-11-01   445.861074  4866.226303 -4455.554864
2005-12-01  -390.237513   923.679907   379.381452
2006-01-01  -755.725402   673.734737   198.968080
2006-02-01  -755.725402   673.734737   198.968080

df2型:

              Value
Date
2005-07-01 -0.07920
2005-08-01 -0.01412
2005-09-01 -0.03646
2005-10-01  0.17432
2005-11-01 -0.05409
2005-12-01  0.04988
2006-01-01 -0.00232

我希望它们有相同的索引,以便df1保持如下:

                     0            1            2
Date                                                                          
2005-07-01          NaN          NaN          NaN
2005-08-01          NaN          NaN          NaN
2005-09-01          NaN          NaN          NaN
2005-10-01   772.142457  5600.491978 -5522.102692  
2005-11-01   445.861074  4866.226303 -4455.554864
2005-12-01  -390.237513   923.679907   379.381452
2006-01-01  -755.725402   673.734737   198.968080
2006-02-01  -755.725402   673.734737   198.968080

df2保持这样:

              Value
Date
2005-07-01 -0.07920
2005-08-01 -0.01412
2005-09-01 -0.03646
2005-10-01  0.17432
2005-11-01 -0.05409
2005-12-01  0.04988
2006-01-01 -0.00232
2006-02-01      NaN

我已经搜索了一段时间来完成这个任务,但是找不到任何函数:(

有人能帮忙吗


Tags: 数据函数datevaluenandf1df2
1条回答
网友
1楼 · 发布于 2024-04-28 21:48:29

您希望^{}并传递另一个df的unioned索引作为新索引,以便对其重新索引:

In [135]:
df.reindex(df1.index.union(df.index))

Out[135]:
                     0            1            2
Date                                            
2005-07-01         NaN          NaN          NaN
2005-08-01         NaN          NaN          NaN
2005-09-01         NaN          NaN          NaN
2005-10-01  772.142457  5600.491978 -5522.102692
2005-11-01  445.861074  4866.226303 -4455.554864
2005-12-01 -390.237513   923.679907   379.381452
2006-01-01 -755.725402   673.734737   198.968080
2006-02-01 -755.725402   673.734737   198.968080

In [134]:
df1.reindex(df.index.union(df1.index))
Out[134]:
              Value
Date               
2005-07-01 -0.07920
2005-08-01 -0.01412
2005-09-01 -0.03646
2005-10-01  0.17432
2005-11-01 -0.05409
2005-12-01  0.04988
2006-01-01 -0.00232
2006-02-01      NaN

您可以先创建组合索引,然后将其传递给上面的对象,以使其更具可读性:

In [136]:
combined_idx = df.index.union(df1.index)
combined_idx

Out[136]:
DatetimeIndex(['2005-07-01', '2005-08-01', '2005-09-01', '2005-10-01',
               '2005-11-01', '2005-12-01', '2006-01-01', '2006-02-01'],
              dtype='datetime64[ns]', name='Date', freq=None)

所以df.reindex(combined_idx)df1.reindex(combined_idx)会起作用

相关问题 更多 >