[英]Using Pandas str.contains using Case insensitive
我的 Dataframe:
Req Col1
1 Apple is a fruit
2 Sam is having an apple
3 Orange is orange
我正在嘗試創建一個僅包含與蘋果相關的數據的新 df。
我的代碼:
Apple=df1[df1.col1.str.contains('apple',case=False)]
獲得正確的 output 無濟於事。 另外,我不想更改完整的 col1 tolower()
它對我有用,我發現的唯一問題是您選擇col1
而不是Col1
。 以下代碼:
df1 = pd.DataFrame({'Req': [1,2,3],
'Col1': ['Apple is a fruit',
'Sam is having an apple',
'Orange is orange']})
Apple=df1[df1.Col1.str.contains('apple',case=False)]
print(Apple)
印刷
>>> Req Col1
>>> 0 1 Apple is a fruit
>>> 1 2 Sam is having an apple
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.