我有一個帶有“分類”列的 pandas DataFrame(視頻游戲列表)。在該列中,我們可以找到:簡單分類:“RPG”或“Action”多個分類:《動作冒險RPG Roguelike》、《Action Shoot'em Up Wargame》你注意到了嗎?沒有分隔符...當(dāng)然,我需要將其拆分為一個新列,帶有分隔符(或具有每個單獨(dú)元素的其他結(jié)構(gòu))。所以"Action Adventure RPG Roguelike" => "Action, Adventure, RPG, Roguelike""Action Shoot'em Up Wargame" => "Action, Shoot'em Up, Wargame"我不能使用空間來分割,也不能使用大寫字母(“ Shoot'em Up”是一個值)。所以,在我看來,我需要創(chuàng)建一個函數(shù)來應(yīng)用于該列,并從值列表中檢查(手工制作),找到所有出現(xiàn)并返回帶有分隔符的字符串......像這樣的東西:classification = ["Action", "Adventure", "RPG", "Roguelike", "Shoot'em Up", "Wargame"...]def magic_tric(data): # do the magic, comparing each classification possible / data return data_separated但我不知道該怎么做。以最有效的方式...有人能幫我嗎...?提前致謝。
1 回答

胡說叔叔
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個贊
這是一個想法..使用str.findall
0
0 Action Adventure RPG Roguelike
1 Action Shoot'em Up Wargame
sep = ["Action", "Adventure", "RPG", "Roguelike", "Shoot'em Up", "Wargame"]
pattern = '|'.join(sep)
pd.DataFrame(df[0].str.findall(pattern).tolist())
0 1 2 3
0 Action Adventure RPG Roguelike
1 Action Shoot'em Up Wargame None
添加回答
舉報
0/150
提交
取消