简体   繁体   English

Pandas dropna 和过滤

[英]Pandas dropna and filtering

I have this dataframe, called "fechadas":我有这个 dataframe,叫做“fechadas”:

print(fechadas)

              CNPJ                                         Favorecido
0    9574957000116                             ccs construtora eireli   
1   73780215000146         d & m empreiteira de projetos e obras ltda   
2   21592015000166                        f t r construtora ltda - me   
3                                                                       
4                                                                       
5   20739399000134      four business desenvolvimento e servicos ltda   
6   20739399000134      four business desenvolvimento e servicos ltda   
7   20739399000134      four business desenvolvimento e servicos ltda   
8   20739399000134      four business desenvolvimento e servicos ltda   
9   17483741000173                      comercial tocantins ltda - me   
10                                                                      
11  17483741000173                      comercial tocantins ltda - me   
12                                                                    

I want to take out the rows where "Favorecido" is empty, so I'm using this code:我想取出“Favorecido”为空的行,所以我使用以下代码:

fechadas=fechadas.dropna(subset=["Favorecido"],axis=0)
fechadas=fechadas.loc[((fechadas['Favorecido'] != "") | (fechadas['Favorecido'] != " ")]
fechadas=fechadas.loc[(len(fechadas['Favorecido']) != 0) | (fechadas['Favorecido'] != True)]

However, when I print fechadas it just looks exactly the same:但是,当我打印 fechadas 时,它看起来完全一样:

              CNPJ                                         Favorecido  \
0    9574957000116                             ccs construtora eireli   
1   73780215000146         d & m empreiteira de projetos e obras ltda   
2   21592015000166                        f t r construtora ltda - me   
3                                                                       
4                                                                       
5   20739399000134      four business desenvolvimento e servicos ltda   
6   20739399000134      four business desenvolvimento e servicos ltda   
7   20739399000134      four business desenvolvimento e servicos ltda   
8   20739399000134      four business desenvolvimento e servicos ltda   
9   17483741000173                      comercial tocantins ltda - me   
10                                                                      
11  17483741000173                      comercial tocantins ltda - me   
12                                                                      

Am I doing something wrong?难道我做错了什么?

I suggest change second condition with Series.str.strip for remove possible more spaces:我建议使用Series.str.strip更改第二个条件以删除可能的更多空格:

fechadas=fechadas[(fechadas['Favorecido'].str.strip() != "")]

If not working, check how looks problematic values:如果不起作用,请检查看起来有问题的值:

print (df.loc[[3,4,10,12], 'Favorecido'].tolist())

I suggest change second condition to: use " = " instead of "!="我建议将第二个条件更改为:使用“=”而不是“!=”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM