[英]Partial String match and vlookup
我有兩個數據幀: df1
和df2
df1:
Column1 Column 2....
The sun rises Why
The earth revolves. Why....
df2:
Column1 Column2
Sun Centre of the earth
Earth Planet
我想要的是將df1
修改為:
df1:
Column1 Column 2 Column3
The sun rises Why Centre of the earth
The earth revolves. Why. Planet
我們可以使用括號來指定列鍵。 in
運算符讓您匹配一個子字符串,然后我們可以對行進行排序。
import pandas as pd
data1 = {
"Column 1": ["The Sun rises", "The earth revolves"],
"Column 2": ["Why","Why"]
}
df1 = pd.DataFrame(data1, columns= ["Column 1", "Column 2"])
data2 = {
"Column 1": ["Sun", "Earth"],
"Column 2": ["Centre of the earth", "Planet"]
}
df2 = pd.DataFrame(data2, columns= ["Column 1", "Column 2"])
df1["Column 3"] = df2["Column 2"]
df3 = df1.apply(lambda j: len([i for i in df1["Column 1"] if j["Column 3"].lower() in i.lower()]) > 0, axis = 1)
df1 = df1[df3 == True]
df1 = df1.sort_values(by="Column 3")
print(df1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.