[英]Extract rows where the lists of columns contain certain values in a pandas 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.