I have a Pandas dataframe ‘df’ like this :
X Y
IX1 IX2
A A1 20 30
A2 20 30
A5 20 30
B B2 20 30
B4 20 30
It lost some rows, and I want to fill in the gap in the middle like this:
X Y
IX1 IX2
A A1 20 30
A2 20 30
A3 NaN NaN
A4 NaN NaN
A5 20 30
B B2 20 30
B3 NaN NaN
B4 20 30
Is there a pythonic way to do this ?
You need to construct your full index, and then use the
reindexmethod of the dataframe. Like so…And then you can use the
fillnamethod to set the NaNs to whatever you want.update (June 2014)
Just had to revisit this myself…
In the current version of pandas, there is a function to build
MultiIndexfrom the Cartesian product of iterables. So the above solution could become:Pretty elegant, in my opinion.