如何从只包含特定值的数据帧中提取行

2024-10-01 11:34:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据集:

| 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

我想提取所有说英语的国家,我认为最简单的方法是提取所有语言中有“英语”一词的国家,理想情况下,我想有一个新的数据框架,列为英语,值为真或假


Tags: and数据english国家countryotherofficiallanguages
1条回答
网友
1楼 · 发布于 2024-10-01 11:34:29

您使用^{}描述的一个实现:

>>> 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

相关问题 更多 >