简体   繁体   English

在 pandas 警告中将 str 转换为浮点数

[英]Converting str to float in pandas warning

I am trying to convert str to float in a pandas data frame and get the following error:我试图在 pandas 数据帧中将 str 转换为 float 并得到以下错误:

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 code:编码:

df_clean['a'] = df_clean['a'].astype(float)

Should I ignore it?我应该忽略它吗? Can it be written without generating the warning?可以在不产生警告的情况下编写它吗? Thanks谢谢

Maybe try using to_numeric也许尝试使用to_numeric

import pandas as pd
df_clean['a'] = pd.to_numeric(df_clean['a'], errors='ignore')

The "errors" flag gives you different options for how to handle ones that cannot be converted. “错误”标志为您提供了不同的选项来处理无法转换的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM