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!
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
                        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()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With