【问题标题】:errors coerce not changing unwanted string to NaN in pandas错误强制不将不需要的字符串更改为熊猫中的 NaN
【发布时间】:2021-01-01 22:37:28
【问题描述】:

我有一个数据集,其中包含不需要的字符串(表示无法进行测量)。当 pandas 读取数据的文本文件时,我想将这些不需要的字符串更改为“NaN”,因为字符串的存在将原本 int 列的数据类型转换为字符串。如果有更好的流程,请告诉我。当我尝试使用我在另一个问题 (Change data type of columns in Pandas) 上找到的方法时,它们会在找到不需要的字符串时中断,否则它们似乎会完全跳过字符串。

代码

import pandas as pd 
data = {
    'ID': [1,2,3,4],
    'V': [6.6,2.01,'tND - 7777',7.01],
    'A': [33,31,'tND - 88881',35]    
    } 
df = pd.DataFrame(data, columns = ['ID','V','A'])

print(df)
df.astype({"V": int})
print(df)
# returns ValueError: invalid literal for int() with base 10: 'tND - 7777'

pd.to_numeric(df['V'], errors = 'coerce')
pd.to_numeric(df['A'], errors = 'coerce')
print(df)
# returns original array, unwanted strings still in place

不需要的字符串

'tND - 7777','tND - 88881'

期望的结果 数据框列中的数据是整数(我假设 NaN 被认为是整数,一旦字符串不再存在,我只需要绘制数据)。

【问题讨论】:

  • pd.to_numeric 返回系列;它对原始基础数据没有影响。您需要重新分配该列。

标签: python pandas string nan


【解决方案1】:

分配输出:

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

另一个想法是使用:

df[['V','A']] = df[['V','A']].apply(pd.to_numeric, errors = 'coerce')

【讨论】:

  • 把我的头砸在桌子上,我怎么没看到!谢谢jezrael,我会尽快接受。
猜你喜欢
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 2023-03-27
相关资源
最近更新 更多