[英]How to replace cells in a Pandas dataframe with multiple columns?
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. 我有一个包含多列的数据框,我想用一列中的前一个值替换0中的单元格。
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. 它适用于
df['A'].replace(to_replace=0, method='ffill')
但是只要它是全数据帧就会抛出错误,可能是因为to_replace
不是一个系列。
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.