Search code examples
pythonpandasindexingdataframereindex

Reshape dataframe to have the same index as another dataframe


I have two dataframes:

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           

and

historicPower

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

I'm trying to reindex the historicPower dataframe to have the same shape as the dayData dataframe (so in this example it would looks like):

   power

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

The dataframes in reality will be alot larger with different shapes.


Solution

  • I think you can use reindex if index has no duplicates:

    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