我有以下資料框df1
string lists
0 i have a dog ['fox', 'dog', 'cat']
1 there is a cat ['dog', 'house', 'car']
2 hello everyone ['hi', 'hello', 'everyone']
3 hi my name is Joe ['name', 'was', 'Joe']
我正在嘗試回傳一個df2
看起來像這樣的資料框
string lists new_string
0 i have a dog ['fox', 'dog', 'cat'] i have a
1 there is a cat ['dog', 'house', 'car'] there is a cat
2 hello everyone ['hi', 'hello', 'everyone']
3 hi my name is Joe ['name', 'was', 'Joe'] hi my is
我已經參考了其他問題,例如https://stackoverflow.com/a/40493603/5879909,但是我在搜索列中的串列而不是預設串列時遇到了麻煩。
uj5u.com熱心網友回復:
考慮到資料框是df
,并且 OP 的目標是創建一個名為的新列new_string
,其值的字串等于列中string
的字串,而列中沒有字串lists
,對于該特定行,以下將完成作業
df['new_string'] = df['string'].apply(lambda x: ' '.join([word for word in x.split() if word not in df['lists'][df['string'] == x].values[0]]))
[Out]:
string lists new_string
0 i have a dog [fox, dog, cat] i have a
1 there is a cat [dog, house, car] there is a cat
2 hello everyone [hi, hello, everyone]
3 hi my name is Joe [name, was, Joe] hi my is
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507953.html
標籤:Python python-3.x 熊猫 细绳 数据框