简体   繁体   English

如何替换所有字符串'? 用 0 浮点数?

[英]How to replace all strings '?' with a 0 float?

I am cleaning a data frame currently and am running into issues because all of them are a mix of int and str, but I am trying to convert all of them to floats.我目前正在清理一个数据框并且遇到了问题,因为它们都是 int 和 str 的混合体,但我正在尝试将它们全部转换为浮点数。 The data frame is all numbers as well as some entries with '?'数据框是所有数字以及一些带有“?”的条目strings that I am trying to replace with '0' floats.我试图用“0”浮点数替换的字符串。 How should I go about doing so?我应该如何 go 这样做呢?

    # Load the data from the file
    df = pd.read_csv('processed.state.csv')
    df.apply(pd.to_numeric) 

Yields an error: Unable to parse string "?"产生错误:无法解析字符串“?” at position 165在 position 165

df = pd.DataFrame([1,23,'1','2', "?"])
df.replace('?', 0).apply(pd.to_numeric)

A more generic solution to replace non-numbers to 0 will be将非数字替换为 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)

You can create your own function:您可以创建自己的 function:

def to_float(item):
    try:
        return float(item)
    except ValueError:
        return 0

And apply that to the DataFrame instead.并将其应用于 DataFrame。

You can use pandas.DataFrame.replace :您可以使用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.

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