I have a dataframe with multiple columns and I would like to replace cells with 0 with the previous value in the column, in one shot.
It works with df['A'].replace(to_replace=0, method='ffill') but as soon as it's the all dataframe it throws an error, probably because to_replace is not a series.
How can I do that ?
import datetime
import pandas as pd
import numpy as np
todays_date = datetime.datetime.now().date()
index = pd.date_range(todays_date-datetime.timedelta(10), periods=4, freq='D')
columns = ['A','B', 'C']
data = np.array([[1, 2, 2], [3, 0, 5], [0, 4, 0], [3, 4, 5]])
df = pd.DataFrame(data, index=index, columns=columns)
df
Out[333]:
A B C
2018-07-16 1 2 2
2018-07-17 3 0 5
2018-07-18 0 4 0
2018-07-19 3 4 5
# Throws an error here :
df.replace(to_replace=0, method='ffill')
TypeError: cannot replace [0] with method ffill on a DataFrame
# Works column by column :
df['A'].replace(to_replace=0, method='ffill')
Out[338]:
2018-07-16 1
2018-07-17 3
2018-07-18 3
2018-07-19 3
Freq: D, Name: A, dtype: int64