![](/img/trans.png)
[英]Replace in dataframe the '%' and then convert all the strings to float
[英]How to replace all strings '?' with a 0 float?
我目前正在清理一个数据框并且遇到了问题,因为它们都是 int 和 str 的混合体,但我正在尝试将它们全部转换为浮点数。 数据框是所有数字以及一些带有“?”的条目我试图用“0”浮点数替换的字符串。 我应该如何 go 这样做呢?
# Load the data from the file
df = pd.read_csv('processed.state.csv')
df.apply(pd.to_numeric)
产生错误:无法解析字符串“?” 在 position 165
df = pd.DataFrame([1,23,'1','2', "?"])
df.replace('?', 0).apply(pd.to_numeric)
将非数字替换为 0 的更通用的解决方案是
def fun(x):
try:
return float(x)
except ValueError:
return 0
df = pd.DataFrame({'c1': [1,23,'1','2', "?"], 'c2': [1,23,'abc','2', "?"]})
df.applymap(fun)
您可以创建自己的 function:
def to_float(item):
try:
return float(item)
except ValueError:
return 0
并将其应用于 DataFrame。
您可以使用pandas.DataFrame.replace :
df = pd.read_csv('processed.state.csv' encoding = 'utf-8')
df.replace('?', 0)
df.apply(pd.to_numeric)
df['col'] = df['col'].map(lambda x: 0.0 if x == '?' else x).astype(np.float64)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.