簡體   English   中英

如何從僅包含某些值的 dataframe 中提取行

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

我有這個數據集:

| 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

我想提取所有講英語的國家,我認為最簡單的方法是提取所有在語言中包含“英語”一詞的國家,理想情況下,我希望有一個新的 dataframe 與列英語和價值觀對或錯。

您使用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