pandas 将字符串转换为浮点表示数据框中的多列

编程入门 行业动态 更新时间:2024-10-20 01:21:09
本文介绍了 pandas 将字符串转换为浮点表示数据框中的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是熊猫的新手,试图弄清楚如何将格式化为字符串的多列转换为float64.目前我正在做下面的事情,但是apply()或applymap()似乎应该能够更加有效地完成此任务……不幸的是,我太菜鸟了,无法弄清楚该怎么做.当前值是百分比格式,格式为"15.5%"

I'm new to pandas and trying to figure out how to convert multiple columns which are formatted as strings to float64's. Currently I'm doing the below, but it seems like apply() or applymap() should be able to accomplish this task even more efficiently...unfortunately I'm a bit too much of a rookie to figure out how. Currently the values are percentages formatted as strings like '15.5%'

for column in ['field1', 'field2', 'field3']: data[column] = data[column].str.rstrip('%').astype('float64') / 100

推荐答案

从0.11.1开始(本周推出),replace具有一个新选项来替换为正则表达式,因此这成为可能

Starting in 0.11.1 (coming out this week), replace has a new option to replace with a regex, so this becomes possible

In [14]: df = DataFrame('10.0%',index=range(100),columns=range(10)) In [15]: df.replace('%','',regex=True).astype('float')/100 Out[15]: <class 'pandas.core.frame.DataFrame'> Int64Index: 100 entries, 0 to 99 Data columns (total 10 columns): 0 100 non-null values 1 100 non-null values 2 100 non-null values 3 100 non-null values 4 100 non-null values 5 100 non-null values 6 100 non-null values 7 100 non-null values 8 100 non-null values 9 100 non-null values dtypes: float64(10)

更快一点

In [16]: %timeit df.replace('%','',regex=True).astype('float')/100 1000 loops, best of 3: 1.16 ms per loop In [18]: %timeit df.applymap(lambda x: float(x[:-1]))/100 1000 loops, best of 3: 1.67 ms per loop

更多推荐

pandas 将字符串转换为浮点表示数据框中的多列

本文发布于:2023-10-27 11:12:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浮点   字符串   转换为   框中   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!