英文:
How to convert pandas column to numeric if there are strings?
问题
- Numerical Values -> Float
 - Empty Values -> N/A
 - 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:
- Numerical Values -> Float
 - Empty Values -> N/A
 - 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论