Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Fill in missing indexes with specific ordered values that are already in column.

I have extracted a one-column dataframe with specific values. Now this is what the dataframe looks like:

           Commodity  
0              Cocoa
4             Coffee
6              Maize
7               Rice
10             Sugar
12             Wheat

Now I want to respectively fill each index that has no value with the value above it in the column so It should look something like this:

            Commodity  
0               Cocoa
1               Cocoa
2               Cocoa
3               Cocoa
4              Coffee
5              Coffee
6               Maize
7                Rice
8                Rice
9                Rice
10              Sugar
11              Sugar
12              Wheat

I don't seem to get anything from the pandas documentation Working with Text Data. Thanks for your help!

like image 575
Dan201 Avatar asked Sep 11 '25 19:09

Dan201


2 Answers

I create a new index with pd.RangeIndex. It works like range so I need to pass it a number one greater than the max number in the current index.

df.reindex(pd.RangeIndex(df.index.max() + 1)).ffill()

   Commodity
0      Cocoa
1      Cocoa
2      Cocoa
3      Cocoa
4     Coffee
5     Coffee
6      Maize
7       Rice
8       Rice
9       Rice
10     Sugar
11     Sugar
12     Wheat
like image 111
piRSquared Avatar answered Sep 14 '25 10:09

piRSquared


First expand the index to include all numbers

s = pd.Series(['Cocoa', 'Coffee', 'Maize', 'Rice', 'Sugar', 'Wheat',], index=[0,4,6,7,10, 12], name='Commodity')
s = s.reindex(range(s.index.max() + 1))

Then do a backfill

s.bfill()
like image 32
Maarten Fabré Avatar answered Sep 14 '25 08:09

Maarten Fabré