检查熊猫数据框架列(字符串/对象)是否是数字(忽略空/null/nan)

发布于 2025-01-18 07:14:21 字数 720 浏览 2 评论 0原文

我尝试这样做的灵感来自于已接受的答案 这里

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携君以终年 2025-01-25 07:14:21

您可以去除空格并将空字符串转换为 NaN,然后​​删除它;然后进行测试:

out = pd.to_numeric(df['col1'].str.strip().replace('', pd.NA).dropna(), errors='coerce').notna().all().item()

输出:

True

此测试对于以下输入抛出 False:

df = pd.DataFrame({'col1':['1', 's']})

You could strip white spaces and convert empty string to NaN, then drop it; then do the test:

out = pd.to_numeric(df['col1'].str.strip().replace('', pd.NA).dropna(), errors='coerce').notna().all().item()

Output:

True

This test throws False for the following input:

df = pd.DataFrame({'col1':['1', 's']})
横笛休吹塞上声 2025-01-25 07:14:21

根据

>>df = pd.DataFrame({'col1':['1', '']})
>>df.col1.str.isnumeric()

0     True
1    False

According to the official pandas doc, you can check a series of data if it's numeric or not.

>>df = pd.DataFrame({'col1':['1', '']})
>>df.col1.str.isnumeric()

0     True
1    False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文