3 回答

TA貢獻1797條經驗 獲得超4個贊
與其刪除包含其他字母的行,不如應用一個函數來獲取包含' GA' 的行:
new = df[df['Campaign'].apply(lambda x: 'GA' in x)]

TA貢獻1810條經驗 獲得超4個贊
這里的假設是對于所有相關行,GA
是在句子的開頭。Pandas str?startswith可以在這里提供幫助:
df.loc[df.Campaign.str.startswith("GA")]
? ? Date? ? Campaign
0? ?3/24/20 GA Shoes Search Campaign
1? ?3/24/20 GA Shoes Display Campaign
2? ?3/24/20 GA Bag Search Campaign
3? ?3/24/20 GA Bag Display Campaign
但是,如果GA可能嵌入在句子中而不是在最開頭,那么如果您提供與此類似的數據將會很有幫助。這樣,就可以確定它GA是在單詞中,還是單獨存在,或者其他什么,并希望找到適合的解決方案

TA貢獻1775條經驗 獲得超8個贊
設置示例數據框
如果你有一個數據框:
df = pd.DataFrame({'x': ['A0', 'A1', 'B2', 'A3'],
? ? ? ? ? ? ? ? ? ?'y': ['B0', 'B1', 'B2', 'B3'],
? ? ? ? ? ? ? ? ? ?'z': ['A0', 'C1', 'C2', 'C3'],
? ? ? ? ? ? ? ? ? ?'w': ['D0', 'D1', 'D2', 'D3']},
? ? ? ? ? ? ? ? ? ? index=[0, 1, 2, 3])
看起來像:
假設您要創建包含A
在 column 中的那些行x
。
方法
str.contains
:
你可以做:
df[df['x'].str.contains('A')]
列表理解
df[['A' in each for each in df['x']]]
就足夠了。
apply()
:
如果你喜歡apply()
,可以這樣做:
df[df['x'].apply(lambda?x:?'A'?in?x)]
結果
所有這些方法都會給你:
最后說明?更一般地說:
方法
str.contains
:
df[df[name_of_column_which_should_contain_something].str.contains(what_should_it_contain)]
列表理解方法:
df[[what_to_search_for in each for each in df[whichcolumn]]]
方法
apply()
:df[df[which_column_to_search_in].apply(lambda x: what_to_search_for in x)]
添加回答
舉報