【发布时间】:2021-08-30 20:00:53
【问题描述】:
假设我有以下 DataFrame:
df_amazon = pd.DataFrame ({
'order_no':[1, 2, 3],
'category':['Books', 'Toys', 'Books'],
'seller':['Amazon.com', 'AZ Toys', 'amazon'] })
如果卖家列中与亚马逊相关,我想获取所有唯一值。
在 SQL 中,有一个 LIKE 关键字。例如:
SELECT seller FROM df_amazon WHERE LOWER(seller) LIKE 'amazon%'
上面的代码会返回所有卖家列中小写字母以“amazon”开头的记录。
Pandas 中有类似的东西吗?
我尝试了以下方法:
df_amazon.loc[df_amazon.seller.str.contains('amazon')]
但这在第三行严格匹配,第一行省略了“Amazon.com”。
然后我尝试了以下方法:
df_amazon.loc[df_amazon.seller.str.lower().contains('amazon')]
但这会返回错误:AttributeError: 'Series' object has no attribute 'contains'
我想快速获取卖家为亚马逊的所有唯一值。
【问题讨论】: