更改 Pandas 数据帧类型时的异常处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23602061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:02:17  来源:igfitidea点击:

Exception handling when changing Pandas dataframe type

pythonpandas

提问by lmart999

I have a Pandas dataframe with a single column of strings. I want to convert the column data to float. Some of the values cannot be converted to float due to their format. I want to omit these "illegal strings" from the result and only extract values that can be legally re-cast as floats. The starting data:

我有一个带有单列字符串的 Pandas 数据框。我想将列数据转换为浮动。由于格式的原因,某些值无法转换为浮点数。我想从结果中省略这些“非法字符串”,只提取可以合法地重新转换为浮点数的值。起始数据:

test=pd.DataFrame()
test.loc[0,'Value']='<3'
test.loc[1,'Value']='10'
test.loc[2,'Value']='Detected'
test.loc[3,'Value']=''

The desired output contains only strings that could be re-cast as floats (in this case, 10):

所需的输出仅包含可以重新转换为浮点数的字符串(在本例中为 10):

cleanDF=test['Value'].astype(float)
cleanDF
0    10
Name: Value, dtype: float64

Of course, this throws an error as expected on the illegal string for float conversion:

当然,这会在浮点转换的非法字符串上按预期抛出错误:

ValueError: could not convert string to float: <3

Is there a simple way to solve this if the dataframe is large and contains many illegal strings in 'Value'?

如果数据框很大并且“值”中包含许多非法字符串,是否有一种简单的方法可以解决这个问题?

Thanks.

谢谢。

采纳答案by Phil

You could try using DataFrame's apply. Write a function that includes an exception handler and apply it to the DataFrame.

您可以尝试使用 DataFrame 的apply. 编写一个包含异常处理程序的函数并将其应用于 DataFrame。

def test_apply(x):
    try:
        return float(x)
    except ValueError:
        return None

cleanDF = test['Value'].apply(test_apply).dropna()