簡體   English   中英

熊貓-替換/刪除一列中每一行中另一列中的部分

[英]pandas - replace/delete part from every row in one column that is in other column

我有2列-df [“ _ 1”]和df [“ _ 2”]。 DF [ “_ 1”]:

1. ananas patrzy
2. socja lizmzwyci ęży

DF [ “_ 2”]:

1. patrzy
2. ęży

我想做的是刪除df_1中位於df_2相關行中的那些部分。 所以結果應該像這樣:

DF [ “_ 3”]

1. ananas
2. socja lizmzwyci

我試過的

1. df.apply(lambda x:  x["_1"].replace(x["_2"], ''),axis=1) # doesn't work
2. df["_1"].str.strip( df["_2"].str) # - tried also with rstrip - doesn't work either 

你有什么想法?

您需要的公式是:

df['_1'] = df.apply(lambda row: row['_1'].replace(row['_2'], ''), axis='columns')

要刪除任何前導/尾隨空格,請添加.str.strip()

df['_1'] = df.apply(lambda row: row['_1'].replace(row['_2'], ''), axis='columns').str.strip()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM