背景--如果所有3x条件都满足的话,我正在尝试从熊猫数据栏(exceptions_df)中删除行。
条件-
Ownership Audit Note列值包含ignore或Ignore.Entity ID %列值是== Account # %的部分字符串值(该列格式为float64).% Ownership coumn is == 100 )。(该列被格式化为float64)提取自dataframe -
% Ownership Ownership Audit Note Entity ID % Account # %
0 100.00 [ignore] 100% Ownership 0.0000000 0.0000000
1 100.00 [ignore] 100% Ownership 0.0000000 0.0000000
2 100.00 [ignore] 100% Ownership 0.0000000 0.0000000
3 100.00 [ignore] 100% Ownership 0.0000000 0.0000000
4 100.00 [ignore] 100% Ownership 0.0000000 0.0000000
5 100.00 [ignore] 100% Ownership 1.0000000 1.0000000
8 100.00 [ignore] 100% Ownership 0.0000234 0.0000234
9 100.00 [ignore] 100% Ownership 0.0000000 0.0000000我的代码-
exceptions_df = exceptions_df[~exceptions_df['Ownership Audit Note'].str.contains('ignore'|'Ignore') &
[~exceptions_df['% Ownership'] == 100] &
[~exceptions_df['Account # %'] == 'Entity ID %']]问题--我似乎得到了下面的TypeError:,它引用了上面的代码行。我错过什么明显的东西了吗?奇怪的是,如果我只包含第一条条件/第一行代码,那么它就能正常工作了!
TypeError: unsupported operand type(s) for |: 'str' and 'str'发布于 2022-01-20 22:57:13
需要删除.contains()中的内部引号。例如,制作了虚拟df。
exceptions_dict = {'% Ownership': {0: 100.0,
1: 100.0,
2: 100.0,
3: 100.0,
4: 100.0,
5: 100.0,
6: 100.0,
7: 100.0,
8: 90.0,
9: 100.0},
'Ownership Audit Note': {0: '[ignore] 100% Ownership',
1: '[ignore] 100% Ownership',
2: '[ignore] 100% Ownership',
3: '[ignore] 100% Ownership',
4: '[ignore] 100% Ownership',
5: '[ignore] 100% Ownership',
6: '[ignore] 100% Ownership',
7: '[ignore] 100% Ownership',
8: 'foo',
9: 'foo'},
'Entity ID %': {0: 0.0,
1: 0.0,
2: 0.0,
3: 0.0,
4: 0.0,
5: 1.0,
6: 2.34e-05,
7: 0.0,
8: 1.0,
9: 1.0},
'Account # %': {0: 0.0,
1: 0.0,
2: 0.0,
3: 0.0,
4: 0.0,
5: 1.0,
6: 2.34e-05,
7: 0.0,
8: 2.0,
9: 2.0}}
exceptions_df = pd.DataFrame(exceptions_dict)
exceptions_df = exceptions_df[(~(exceptions_df['Ownership Audit Note'].str.contains('ignore|Ignore'))) &
(~(exceptions_df['% Ownership'] == 100.0)) &
(~(exceptions_df['Account # %'] == 'Entity ID %'))]
print(exceptions_df)
% Ownership Ownership Audit Note Entity ID % Account # %
8 90.0 foo 1.0 2.0发布于 2022-01-20 22:33:47
使用错误的分区括号。让我们试试
exceptions_df = exceptions_df[(~(exceptions_df['Ownership Audit Note'].str.contains('ignore'|'Ignore'))) &
(~(exceptions_df['% Ownership'] == 100)) &
( ~(exceptions_df['Account # %'] == 'Entity ID %'))]https://stackoverflow.com/questions/70794195
复制相似问题