Q. How do you fill all missing values with previous non-missing values?


Answer :-


To fill all missing values with the previous non-missing value in a Pandas DataFrame or Series, you can use the fillna() method with the method parameter set to 'ffill'.

import pandas as pd

#series with missing values
s = pd.Series([1, 2, None, 4, None, 6])

# fill missing values with the previous non-missing value
s_filled = s.fillna(method='ffill')

# print the original and filled series
print('Original Series:\n', s)
print('\nFilled Series:\n', s_filled)


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post