You can use the fillna()
method to replace missing values based on previous or next values in a pandas DataFrame. Here's an example:
main.py281 chars13 lines
Output:
main.py98 chars8 lines
In this example, method='ffill'
replaces the missing values with the previous value (i.e., forward fill) in each column. You can also use method='bfill'
to replace missing values with the next value (i.e., backward fill).
Note that fillna()
returns a new DataFrame by default, but we set inplace=True
to modify the original DataFrame.
gistlibby LogSnag