简体   繁体   English

如何从仅包含某些值的 dataframe 中提取行

[英]How to extract rows from a dataframe that contain only certain values

I have this data set:我有这个数据集:

| Country |Languages Spoken |


|  Afghanistan  |   Dari Persian, Pashtu (both official), other Turkic and minor languages      
|  Algeria      | Arabic (official), French, Berber dialects                                    
|Andorra        | Catalán (official), French, Castilian, Portuguese                             
|Angola         | Portuguese (official), Bantu and other African languages                      
|Antigua and Barbuda |  English (official), local dialects 

|Australia      |          English 79%, native and other languages

and I want to extract all the english speeaking countries, I think the easiest way would be to extract all the countries that have the word 'English' in the languages, ideally i want to have a new dataframe with the column english speaking and with values true or false.我想提取所有讲英语的国家,我认为最简单的方法是提取所有在语言中包含“英语”一词的国家,理想情况下,我希望有一个新的 dataframe 与列英语和价值观对或错。

One implementation of what you describe using pandas.Series.str.contains :您使用pandas.Series.str.contains描述的一种实现:

>>> df
               Country                                   Languages Spoken
0          Afghanistan  Dari Persian, Pashtu (both official), other Tu...
1              Algeria         Arabic (official), French, Berber dialects
2              Andorra  Catalán (official), French, Castilian, Portuguese
3               Angola  Portuguese (official), Bantu and other African...
4  Antigua and Barbuda                 English (official), local dialects
5            Australia            English 79%, native and other languages
>>>
>>> >>> df['English speaking'] = df['Languages Spoken'].str.contains('English')
>>> df
               Country                                   Languages Spoken  English speaking
0          Afghanistan  Dari Persian, Pashtu (both official), other Tu...             False
1              Algeria         Arabic (official), French, Berber dialects             False
2              Andorra  Catalán (official), French, Castilian, Portuguese             False
3               Angola  Portuguese (official), Bantu and other African...             False
4  Antigua and Barbuda                 English (official), local dialects              True
5            Australia            English 79%, native and other languages              True

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

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