将百分比字符串转换为浮点数在 pandas 中read_csv [英] Convert percent string to float in pandas read_csv

查看:96
本文介绍了将百分比字符串转换为浮点数在 pandas 中read_csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在熊猫中使用read_csv时,是否可以将"34%"之类的值直接转换为int或float?我希望将其直接读取为0.34.

Is there a way to convert values like '34%' directly to int or float when using read_csv in pandas? I would like that it is directly read as 0.34.

在read_csv中使用它不起作用:

Using this in read_csv did not work:

read_csv(..., dtype={'col':np.float})

将csv加载为'df'后,这也无法正常工作,并显示错误"float()的无效文字:34%"

After loading the csv as 'df' this also did not work with the error "invalid literal for float(): 34%"

df['col'] = df['col'].astype(float)

我最终使用了这个可行但长期困扰的东西:

I ended up using this which works but is long winded:

df['col'] = df['col'].apply(lambda x: np.nan if x in ['-'] else x[:-1]).astype(float)/100

推荐答案

您可以定义一个自定义函数,将百分比转换为浮点数

You can define a custom function to convert your percents to floats

In [149]:
# dummy data
temp1 = """index col 
113 34%
122 50%
123 32%
301 12%"""
# custom function taken from https://stackoverflow.com/questions/12432663/what-is-a-clean-way-to-convert-a-string-percent-to-a-float
def p2f(x):
    return float(x.strip('%'))/100
# pass to convertes param as a dict
df = pd.read_csv(io.StringIO(temp1), sep='\s+',index_col=[0], converters={'col':p2f})
df
Out[149]:
        col
index      
113    0.34
122    0.50
123    0.32
301    0.12
In [150]:
# check that dtypes really are floats
df.dtypes
Out[150]:
col    float64
dtype: object

我的浮动代码百分比是ashwini的回答:

My percent to float code is courtesy of ashwini's answer: What is a clean way to convert a string percent to a float?

这篇关于将百分比字符串转换为浮点数在 pandas 中read_csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆