部分字符串匹配和vlookup

时间:2019-07-16 10:29:44

标签: python

我有两个数据框:df1df2

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

1 个答案:

答案 0 :(得分:0)

我们可以使用方括号指定列键。 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)