重塑数据帧以使其具有与另一个数据帧相同的索引

时间:2016-08-31 11:24:15

标签: python pandas indexing dataframe reindex

我有两个数据帧:

dayData


        power_comparison      final_average_delta_power calculated_power
1                    0.0               0.0                  0       
2                    0.0               0.0                  0           
3                    0.0               0.0                  0           
4                    0.0               0.0                  0       
5                    0.0               0.0                  0           
7                    0.0               0.0                  0           

historicPower

   power
0    0.0
1    0.0
2    0.0
3   -1.0
4    0.0
5    1.0
7    0.0

我尝试重新索引historicPower数据框,使其具有与dayData数据框相同的形状(因此在此示例中看起来如此):

   power

1    0.0
2    0.0
3   -1.0
4    0.0
5    1.0
7    0.0

现实中的数据帧会因形状不同而变得更大。

1 个答案:

答案 0 :(得分:3)

如果index没有重复项,我认为你可以使用reindex

historicPower = historicPower.reindex(dayData.index) 
print (historicPower)
   power
1    0.0
2    0.0
3   -1.0
4    0.0
5    1.0
7    0.0
相关问题