简体   繁体   English

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

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

I was practicing data wrangling and I eneded up with this simple dataset.我正在练习数据整理,并使用了这个简单的数据集。 but then I started to filter and selecting some information on it but is not working但后来我开始过滤并选择一些信息但不工作

here is the data set:这是数据集:

https://drive.google.com/file/d/1d1FMWhh3U1KnfVFYyC5R5USuB2BbcN6S/view?usp=sharing 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

We see here that Accenture is in the second row but when I try to call it is not working我们在这里看到埃森哲在第二排,但是当我尝试调用它时它不起作用

df['campany_name'] == 'Accenture'

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

I don't really want to get a different way.我真的不想换一种方式。 I just want to understand what is happening under the hood and fully understand what is different in this data set that I can't just do it like I normaly do.我只是想了解幕后发生的事情,并完全了解这个数据集中有什么不同,我不能像往常那样做。 which is df['campany_name] == 'Accenture' I should get boolenans, and with those id be able to get the row doing df[df['campany_name] == 'Accenture']这是 df['campany_name] == 'Accenture' 我应该得到布尔值,并且有了这些 id 就可以得到执行 df[df['campany_name] == 'Accenture'] 的行

something must be wrong at the index or format level.索引或格式级别一定有问题。 but I mean i'm new to python.但我的意思是我是 python 的新手。

Do

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

and then you can try:然后你可以尝试:

df.query('campany_name == Accenture')

or或者

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

and if you know the row and column and you are trying to retrieve just one value you can do:如果你知道行和列,并且你试图只检索一个值,你可以这样做:

df.at[1, 'campany_name']

Also, remember that you are just printing information, if you need to save the result, assign it to something eg:另外,请记住,您只是在打印信息,如果您需要保存结果,请将其分配给例如:

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

As you are trying to filter the dataframe given only a string, you can use df.Series.str.contains当您尝试过滤仅给定字符串的 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