python - 删除包含任何数字子字符串的列行

标签 python python-2.7 pandas delete-row isnumeric

我注意到,当 Pandas DataFrame 中的列元素具有数字子字符串时,方法 isnumeric 返回 false。

例如:

row 1, column 1 has the following: 0002 0003 1289
row 2, column 1 has the following: 89060 324 123431132
row 3, column 1 has the following: 890GB 32A 34311TT
row 4, column 1 has the following: 82A 34311TT
row 4, column 1 has the following: 82A 34311TT 889 9999C

显然,第 1 行和第 2 行都是数字,但是 isnumeric 对第 1 行和第 2 行返回 false。

我找到了一个解决方法,涉及将每个子字符串分成它们自己的列,然后为每个子字符串创建一个 bool 值列以将 bool 值加在一起以显示一行是否全部为数字。然而,这很乏味,而且我的功能看起来也不整洁。我也不想去除和替换空格(将所有子字符串压缩成一个数字),因为我需要保留原始子字符串。

有谁知道一种更简单的解决方案/技术可以正确地告诉我这些具有一个或多个数字子字符串的元素都是数字的?我的最终目标是删除这些仅包含数字的行。

最佳答案

我认为需要使用 splitall 来检查所有数字字符串的列表理解:

mask = ~df['a'].apply(lambda x: all([s.isnumeric() for s in x.split()]))

mask = [not all([s.isnumeric() for s in x.split()]) for x in df['a']]

如果要检查至少一个数字字符串是否使用any:

mask = ~df['a'].apply(lambda x: any([s.isnumeric() for s in x.split()]))

mask = [not any([s.isnumeric() for s in x.split()]) for x in df['a']]

关于python - 删除包含任何数字子字符串的列行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49516442/

相关文章:

python - 构建时选择 GCC 版本 ( setup.py )

python - sqlite3.操作错误: near "X": syntax error in CREATE TABLE statement

python - 将 apscheduler 从 3.0.1 升级到 3.1.0

python - 这种方法是否有更快的替代方法来从字典列表中获取最后更新消息?

python - 如何使用 Pandas 仅在值之间进行插值(在列中最后一个 NaN 之前和之后停止)?

python - 清理 Pandas 数据框中的 URL 列

python - 从在线打开图像,保存到服务器 Flask

python - 在python中复制选定的文件

python - Pandas 从 python 中的日期字符串列获取日期值

pandas - 在单元测试中创建 Pandas 数据框