首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >熊猫的数据行不会掉

熊猫的数据行不会掉
EN

Stack Overflow用户
提问于 2022-01-20 22:29:05
回答 2查看 112关注 0票数 1

背景--如果所有3x条件都满足的话,我正在尝试从熊猫数据栏(exceptions_df)中删除行。

条件-

  1. Ownership Audit Note列值包含ignoreIgnore.
  2. Entity ID %列值是== Account # %的部分字符串值(该列格式为float64).
  3. % Ownership coumn is == 100 )。(该列被格式化为float64)

提取自dataframe -

代码语言:javascript
复制
  % 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

我的代码-

代码语言:javascript
复制
exceptions_df = exceptions_df[~exceptions_df['Ownership Audit Note'].str.contains('ignore'|'Ignore') & 
                             [~exceptions_df['% Ownership'] == 100] & 
                             [~exceptions_df['Account # %'] == 'Entity ID %']]

问题--我似乎得到了下面的TypeError:,它引用了上面的代码行。我错过什么明显的东西了吗?奇怪的是,如果我只包含第一条条件/第一行代码,那么它就能正常工作了!

代码语言:javascript
复制
TypeError: unsupported operand type(s) for |: 'str' and 'str'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-20 22:57:13

需要删除.contains()中的内部引号。例如,制作了虚拟df。

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2022-01-20 22:33:47

使用错误的分区括号。让我们试试

代码语言:javascript
复制
exceptions_df = exceptions_df[(~(exceptions_df['Ownership Audit Note'].str.contains('ignore'|'Ignore'))) & 
                             (~(exceptions_df['% Ownership'] == 100)) & 
                            ( ~(exceptions_df['Account # %'] == 'Entity ID %'))]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70794195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档