Reputation: 181
SS RR
10.4
12.6
5.6
8.7
I want to fill in the blank rows with preceding values.
Intended result:
SS RR
10.4
12.6 10.4
12.6 10.4
12.6 5.6
8.7 5.6
8.7 5.6
I am trying to first replace the blank values with NaN and then the iteration of preceding values:
df[df['SS']==""] = np.NaN
df[df['RR']==""] = np.NaN
df.SS.fillna(method='ffill')
df.RR.fillna(method='ffill')
But for some reason it doesn't make any difference to the dataframe. What am i missing here? Thanks!
EDIT:
What if I want to ffill
within the respective 'ID':
ID SS RR
ABC 10.4
ABC 12.6
ABC
LMN 5.6
LMN 8.7
Intended result:
ID SS RR
ABC 10.4
ABC 12.6 10.4
ABC 12.6 10.4
LMN 5.6
LMN 8.7 5.6
Do i use the groupby
function and where to add it to make things work? Thanks again!
Upvotes: 0
Views: 184
Reputation: 34046
You don't need fillna
here, you can call ffill()
directly:
In [1793]: df[['SS', 'RR']] = df[['SS', 'RR']].ffill()
In [1794]: df
Out[1794]:
SS RR
0 NaN 10.4
1 12.6 10.4
2 12.6 10.4
3 12.6 5.6
4 8.7 5.6
Upvotes: 1
Reputation: 13407
fillna
does not operate inplace. You'll either need to add inplace=True
to your calls to fillna
as another answer has indicated.
Alternatively you can rewrite your code to overwrite your dataframe with the corrected output.
df = df.replace("", np.nan).ffill()
.replace("", np.nan)
will replace all blanks with NaN
throughout your entire dataframe.ffill()
is a shortcut for .fillna(method="ffill")
Since we're not specifying the inplace=True
flag here, we can chain these methods together and overwrite our dataframe (or produce a new dataframe with these alterations by changing our variable name:
clean_df = df.replace("", np.nan).ffill()
Upvotes: 1
Reputation: 323226
You should assign it back or add inplace
df.SS.fillna(method='ffill',inplace=True)
df.RR.fillna(method='ffill',inplace=True)
#df.SS = df.SS.fillna(method='ffill')
#df.RR = df.RR.fillna(method='ffill')
Upvotes: 2