检查熊猫数据框架列(字符串/对象)是否是数字(忽略空/null/nan)
我尝试这样做的灵感来自于已接受的答案 这里:
df = pd.DataFrame({'col1':['1', '']})
all_numeric = pd.to_numeric(df['col1'], errors='coerce').notnull().all().item()
print(all_numeric)
检测列是否为数字(忽略空白+ NULL + NAN)。
在上面的代码中,all_numeric 是 False (Python bool),这没有意义,或者也许有意义?我想我尝试估算 nan,因为原因可能是空值:
df = pd.DataFrame({'col1':['1', '']})
df = df.apply(lambda x: x.str.strip()).replace('', np.nan)
all_numeric = pd.to_numeric(df['col1'], errors='coerce').notnull().all().item()
print(all_numeric)
相同的结果。也许我检查列的所有值是否都是数字(忽略 NULL/NAN/空字符串)的方法是错误的?谢谢!
I tried this inspired by the accepted answer here:
df = pd.DataFrame({'col1':['1', '']})
all_numeric = pd.to_numeric(df['col1'], errors='coerce').notnull().all().item()
print(all_numeric)
to detect that a columns is numeric (ignoring blanks + NULLs + NANs).
In the above code all_numeric is False (Python bool), which does not make sense or maybe it does? I thought I try to impute nan as the reason might be the empty value:
df = pd.DataFrame({'col1':['1', '']})
df = df.apply(lambda x: x.str.strip()).replace('', np.nan)
all_numeric = pd.to_numeric(df['col1'], errors='coerce').notnull().all().item()
print(all_numeric)
Same outcome. Maybe my way of checking if all values of a column are numeric (ignoring NULL/NAN/empty strings) is wrong? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以去除空格并将空字符串转换为 NaN,然后删除它;然后进行测试:
输出:
此测试对于以下输入抛出 False:
You could strip white spaces and convert empty string to NaN, then drop it; then do the test:
Output:
This test throws False for the following input:
根据
According to the official pandas doc, you can check a series of data if it's numeric or not.