[英]Best way to replace column values in pandas?
What is the best way to such an operation:这种操作的最佳方法是什么:
data[col] = data[col].fillna(0)
I just want to replace the values of a column without raising the warning:我只想替换列的值而不发出警告:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
The suggestion works:该建议有效:
d = {'date': ['2020-01','2020-01','2020-02','2020-02','2020-03','2020-03','2020-04','2020-04',], 'A': [1,2,np.nan,4,5,6,7,8]}
df = pd.DataFrame(data=d)
print(df)
date A
0 2020-01 1.0
1 2020-01 2.0
2 2020-02 NaN
3 2020-02 4.0
4 2020-03 5.0
5 2020-03 6.0
6 2020-04 7.0
7 2020-04 8.0
df.loc[df['A'].isna(),'A']=0
print(df)
date A
0 2020-01 1.0
1 2020-01 2.0
2 2020-02 0.0
3 2020-02 4.0
4 2020-03 5.0
5 2020-03 6.0
6 2020-04 7.0
7 2020-04 8.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.