python - 根据列中值的前缀删除 Pandas 行

标签 python python-2.7 pandas

我有一个数据框,df,其中有一列名为帐号。我正在尝试删除帐户前两个字母为“AA”或“BB”的行

import pandas as pd

df = pd.DataFrame(data=["AA121", "AB1212", "BB121"],columns=['Account'])
print df
df = df[df['Account Number'][2:] not in ['AA', 'BB']]

错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

最佳答案

您可以尝试contains :

import pandas as pd

df = pd.DataFrame(data=["AA121", "AB1212", "BB121"],columns=['Account'])
print df
  Account
0   AA121
1  AB1212
2   BB121

print df['Account'].str[:2]
0    AA
1    AB
2    BB
Name: Account, dtype: object

print df['Account'].str[:2].str.contains('AA|BB')
0     True
1    False
2     True
Name: Account, dtype: bool


df = df[~(df['Account'].str[:2].str.contains('AA|BB'))]
print df
  Account
1  AB1212

或者使用 startswith :

print ((df['Account'].str[:2].str.startswith('AA')) |
        (df['Account'].str[:2].str.startswith('BB')))
0     True
1    False
2     True
Name: Account, dtype: bool

print ~((df['Account'].str[:2].str.startswith('AA')) |
        (df['Account'].str[:2].str.startswith('BB')))
0    False
1     True
2    False
Name: Account, dtype: bool

df = df[~((df['Account'].str[:2].str.startswith('AA')) | 
          (df['Account'].str[:2].str.startswith('BB')))]
print df
  Account
1  AB1212

关于python - 根据列中值的前缀删除 Pandas 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35065942/

相关文章:

python - pip3 无法在 ubuntu 20.04 : "has no attribute ' SourceFileLoader'"上运行

Python:凯撒密码

python - 当轴点是 bin 时,如何在 matplotlib 中对轴进行排序?

python - 如何序列化/反序列化这个实体?

python - 使用不同的轴类型绘制多个图

python - 在 Python 2.7 中,如何覆盖单个函数的字符串表示形式?

python - 每当 matplotlib 中两条线相遇时,如何使用 Python 打印某些内容?

python - 添加字符串 + 自动递增 - pandas, python

python - 如何找到低于(或高于)平均值的值

Python configparser 不会接受没有值的键