背景 -exceptions_df如果滿足所有 3x 條件,我正在嘗試從 pandas 資料框 ( ) 中洗掉行。
條件 -
Ownership Audit Note列值包含ignore或的部分字串值Ignore。Entity ID %列值為 ==Account # %(此列的格式為 afloat64)。% Ownership國家是 ==100。(此列的格式為float64)
從資料框中提取 -
% 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'
uj5u.com熱心網友回復:
需要洗掉 .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
uj5u.com熱心網友回復:
使用了錯誤的磁區支架。我們試試吧
exceptions_df = exceptions_df[(~(exceptions_df['Ownership Audit Note'].str.contains('ignore'|'Ignore'))) &
(~(exceptions_df['% Ownership'] == 100)) &
( ~(exceptions_df['Account # %'] == 'Entity ID %'))]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418881.html
標籤:
上一篇:從R中的復雜字串創建多行
