繁体   English   中英

Pandas 选择不根据列中的值过滤行?

[英]Pandas selection not filtering rows based on values in columns?

我正在练习数据整理,并使用了这个简单的数据集。 但后来我开始过滤并选择一些信息但不工作

这是数据集:

https://drive.google.com/file/d/1d1FMWhh3U1KnfVFYyC5R5USuB2BbcN6S/view?usp=sharing

df.head()

0                            TCS 
1                      Accenture 
2                      Cognizant 
3                     ICICI Bank 
4                      HDFC Bank 
                  ...            
8996              Bitla Software 
8997                Kern Liebers 
8998           ANAAMALAIS TOYOTA 
8999                    Elsevier 
9000    Samsung Heavy Industries 
Name: campany_name, Length: 9001, dtype: object

我们在这里看到埃森哲在第二排,但是当我尝试调用它时它不起作用

df['campany_name'] == 'Accenture'

0       False
1       False
2       False
3       False
4       False
        ...  
8996    False
8997    False
8998    False
8999    False
9000    False

我真的不想换一种方式。 我只是想了解幕后发生的事情,并完全了解这个数据集中有什么不同,我不能像往常那样做。 这是 df['campany_name] == 'Accenture' 我应该得到布尔值,并且有了这些 id 就可以得到执行 df[df['campany_name] == 'Accenture'] 的行

索引或格式级别一定有问题。 但我的意思是我是 python 的新手。

df['campany_name'] = df['campany_name'].astype(str)

然后你可以尝试:

df.query('campany_name == Accenture')

或者

df[df['campany_name'] == 'Accenture']

如果你知道行和列,并且你试图只检索一个值,你可以这样做:

df.at[1, 'campany_name']

另外,请记住,您只是在打印信息,如果您需要保存结果,请将其分配给例如:

acc_row = df.query('campany_name == Accenture')

当您尝试过滤仅给定字符串的 dataframe 时,您可以使用df.Series.str.contains

aaa[aaa['campany_name'].str.contains('Accenture')]
 
                       campany_name  ...    jobs interviews
1                        Accenture   ...  4600.0     2500.0
5814    Accenture Federal Services   ...     NaN       20.0

[2 rows x 10 columns]

暂无
暂无

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

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