4

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

2 Answers 2

3

May be this:

print(df.replace(0,np.nan).ffill())

Output:

              A    B    C
2018-07-16  1.0  2.0  2.0
2018-07-17  3.0  2.0  5.0
2018-07-18  3.0  4.0  5.0
2018-07-19  3.0  4.0  5.0
Sign up to request clarification or add additional context in comments.

Comments

2

Which version of pandas are you using? It seems like they added the possibility to use method in a DataFrame with version 0.23.0: see the docs.

1 Comment

Correct, I'm using version 0.22.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.