如何将pandas列转换为数字,如果列中包含字符串?

huangapple go评论75阅读模式
英文:

How to convert pandas column to numeric if there are strings?

问题

  1. Numerical Values -> Float
  2. Empty Values -> N/A
  3. Text Values -> N/A

当我尝试运行 astype('float') 时,我遇到了错误:

import pandas as pd
data = ['5', '4', '3', '', 'NO DATA ', '5']
data = ['5', '4', '3', '', '', '5']
df = pd.DataFrame({'data': data})

df[['data']].astype('float')

我尝试查阅文档和 Stack Overflow,但没有找到如何实现这个目标的方法。

英文:

I have a dataset that has numerical values, empty values and text values. I want to do the following in pandas:

  1. Numerical Values -> Float
  2. Empty Values -> N/A
  3. Text Values -> N/A

When I try to run astype('float'), I get an error:

import pandas as pd
data = ['5', '4', '3', '', 'NO DATA ', '5']
data = ['5', '4', '3', '', '', '5']
df = pd.DataFrame({'data': data})

df[['data']].astype('float')

I've tried to look over the documentation and stackoverflow, but I didn't find out how to do this.

答案1

得分: 1

使用Pandas的to_numeric函数,我们可以将任何有效的值转换为浮点数,同时将无效的值转换为NaN:

import pandas as pd
data = ['5', '4', '3', 'NO DATA', '', '5']
df = pd.DataFrame({'data': data})

df['data'] = pd.to_numeric(df['data'], errors='coerce')

errors='coerce' 确保无效的值被转换为NaN而不是引发错误。

结果将如下所示:

data
0    5.0
1    4.0
2    3.0
3    NaN
4    NaN
5    5.0
英文:

Using panda's to_numeric function, we can turn any valid value into floats, while turning invalid values into NaNs:

import pandas as pd
data = ['5', '4', '3', 'NO DATA', '', '5']
df = pd.DataFrame({'data': data})

df['data'] = pd.to_numeric(df['data'], errors='coerce')

The errors='coerce' makes sure that invalid values are turned into NaN instead of raising an error.

And the result will be:

data
0	5.0
1	4.0
2	3.0
3	NaN
4	NaN
5	5.0

huangapple
  • 本文由 发表于 2023年3月12日 13:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定