[英]Partial String match and vlookup
I have two DataFrames: df1
and df2
我有两个数据帧:
df1
和df2
df1:
Column1 Column 2....
The sun rises Why
The earth revolves. Why....
df2:
Column1 Column2
Sun Centre of the earth
Earth Planet
What I want is df1
to be modified as:我想要的是将
df1
修改为:
df1:
Column1 Column 2 Column3
The sun rises Why Centre of the earth
The earth revolves. Why. Planet
We can use brackets to specify a column key.我们可以使用括号来指定列键。 The
in
operator let's you match a substring and then we can sort the rows. 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.