pandas 数值出现在非数值系列中
pandas numeric value showing up in non numeric Serie
为什么即使在调用 str.strip 之后数值仍被视为非数值?
这是我的情况:
df['ID'] = df['ID'].str.strip()
id = df['ID']
indices = [ i for (i, v) in enumerate(id.str.isnumeric()) if v == False ]
non_numeric = id.filter(indices)
id.head(-5)
And this is the output of id.head(-5):
141 C536379
154 C536383
235 C536391
236 C536391
237 C536391
...
470612 576618
470614 576618
470616 576618
470618 576618
470673 576618
Name: ID, Length: 7892, dtype: object
为什么所有那些不以字母开头的数字仍在 non_numeric 数组中?
起初我认为这是由于前导空格引起的,但后来我添加了 strip() 并没有任何改变。
编辑:我需要分析系列中的非数值,所以我正在尝试提取它们。
查看您最近的编辑,我相信这就是您想要做的:
"EDIT: I need to analyse the non numeric values in the Series, so I'm trying to extracting them."
单看字符串,方法很多。这是一个。使用 pd.to_numeric()
创建一个系列 s
并传递 errors='coerce'
。这将为非数字数据提供 return NaN
值。使用 isnull()
:
从那里将其作为过滤器传递给该系列的 NaN
行的数据框
s = pd.to_numeric(df['ID'], errors='coerce')
df = df[s.isnull()]
df
ID
141 C536379
154 C536383
235 C536391
236 C536391
237 C536391
为什么即使在调用 str.strip 之后数值仍被视为非数值?
这是我的情况:
df['ID'] = df['ID'].str.strip()
id = df['ID']
indices = [ i for (i, v) in enumerate(id.str.isnumeric()) if v == False ]
non_numeric = id.filter(indices)
id.head(-5)
And this is the output of id.head(-5):
141 C536379
154 C536383
235 C536391
236 C536391
237 C536391
...
470612 576618
470614 576618
470616 576618
470618 576618
470673 576618
Name: ID, Length: 7892, dtype: object
为什么所有那些不以字母开头的数字仍在 non_numeric 数组中?
起初我认为这是由于前导空格引起的,但后来我添加了 strip() 并没有任何改变。
编辑:我需要分析系列中的非数值,所以我正在尝试提取它们。
查看您最近的编辑,我相信这就是您想要做的:
"EDIT: I need to analyse the non numeric values in the Series, so I'm trying to extracting them."
单看字符串,方法很多。这是一个。使用 pd.to_numeric()
创建一个系列 s
并传递 errors='coerce'
。这将为非数字数据提供 return NaN
值。使用 isnull()
:
NaN
行的数据框
s = pd.to_numeric(df['ID'], errors='coerce')
df = df[s.isnull()]
df
ID
141 C536379
154 C536383
235 C536391
236 C536391
237 C536391