3
刪除字符串我有一個數據幀df
像這樣:從數據框中的所有項目與大熊貓
dic = {'A':['pap','cdf\nsdc','ert','dgx','kll\nsrw','sdq'],
'B':[1,4,6,2,5,6],
'C':['123\n12','34','55','321\n88','09','45']}
df = pd.DataFrame(dic)
我的目標是通過\n
從字符串形成的所有列撈出什麼,如果它前面:abc\ndef
--- >def
我能夠通過使用以下幾行代碼來實現我的目標:
for index,row in df.iterrows():
df['A'][index]=row['A'].split('\n')[-1]
df['C'][index]=row['C'].split('\n')[-1]
但是,我希望有一個更智能,更緊湊的方式來實現這樣的結果。你能建議一個比我更優雅的方式嗎(也許一些oneliner)?
注意:列B
是浮動!