python - 删除行的正则表达式

标签 python regex pandas

这是一个非常有趣的问题,我正在寻求专家的帮助以更多地了解它以及如何去做。我有一个 DataFrame(我在解析来自 Big Iron 的数据时制作的......仍然存在)。现在我想使用正则表达式删除一些行,但不知道它在 Pandas 中是如何工作的。

24 | DRFT.146.856 |    Dollar-  |  (60.00) | DEBITS-  |  0.00 |  CREDITSDRA- | 0.00   
25 |   0616-21.01 |      2407   |  WAYZAT  |   TMCD   | JUNE  |      16,DRA  |2013   
26 |          AND | CORRECTION  |JOURNAL00 |    <DB>  |KLRETY | CATEGORYDRA- |    *   
27 | DRFT.146.867 |    Dollar-  | (200.00) | DEBITS-  |  0.00 |  CREDITSDRA- | 0.00   
28 | DRFT.146.922 |   Dollar-   | (25.00)  |DEBITS-   | 0.00  | CREDITSDRA-  |0.00   
29 | DRFT.146.963 |    Dollar-  | (100.00) | DEBITS-  |  0.00 |  CREDITSDRA- | 0.00   
30 | DRFT.146.964 |    Dollar-  | (100.00) | DEBITS-  |  0.00 |  CREDITSDRA- | 0.00  

关注的行是 25 和 26,其中数据不遵循任何模式。任何线索。

最佳答案

几个可能的竞争者:

In [11]: df[2].str.contains('Dollar')
Out[11]:
0     True
1    False
2    False
3     True
4     True
5     True
6     True
Name: 2, dtype: bool

In [12]: df[3].str.startswith('(')
Out[12]:
0     True
1    False
2    False
3     True
4     True
5     True
6     True
Name: 3, dtype: bool

做这种事情总是有点黑暗的艺术(因为通常有很多数据,有些看起来可能与好的数据非常相似)...

In [13]: df[df[3].str.startswith('(')]
Out[13]:
    0             1        2         3        4       5            6   7
0  24  DRFT.146.856  Dollar-    (60.00)  DEBITS-   0.00  CREDITSDRA-   0
3  27  DRFT.146.867  Dollar-   (200.00)  DEBITS-   0.00  CREDITSDRA-   0
4  28  DRFT.146.922  Dollar-    (25.00)  DEBITS-   0.00  CREDITSDRA-   0
5  29  DRFT.146.963  Dollar-   (100.00)  DEBITS-   0.00  CREDITSDRA-   0
6  30  DRFT.146.964  Dollar-   (100.00)  DEBITS-   0.00  CREDITSDRA-   0

关于python - 删除行的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17283954/

相关文章:

python - Django @override_settings 不允许字典?

python - 如何在 powerBI 上运行 Python 脚本

python - Flask - WSGI - 没有名为 'flask' 的模块

javascript - 正则表达式检查国际电话号码的开头

regex - JISON:为什么在正则表达式中使用 "."而不是\.?

javascript - 如何用正则表达式替换 Javascript 中的 "value"?

python - 在 Python 中从具有多个多元时间序列的数据帧创建数组数组

python - Numpy 匹配索引维度

python - Python Pandas 中的 GroupBy 函数,如 SUM(col_1*col_2)、加权平均值等

python - Pandas - 正确使用 Apply 函数