Python/Pandas:如果匹配,则将一个 df 中的值添加到另一个 df 中的行尾
Python/Pandas: add value from one df to end of row in another df if there is a match
我需要 return df1 中一列的值,如果 df2 中的值在第一行,则将其附加到 df2 中的一行。
示例代码
df1 = pd.DataFrame(
{
'terms' : ['term1','term2'],
'code1': ['1234x', '4321y'],
'code2': ['2345x','5432y'],
'code3': ['3456x','6543y']
}
)
df1 = df1[['terms'] + df1.columns[:-1].tolist()]
df2 = pd.DataFrame(
{
'name': ['Dan','Sara','Conroy'],
'rate': ['3','3.5','5.2'],
'location': ['FL','OH','NM'],
'code': ['4444g','6543y','2345x']
})
df2 = df2[['name','rate','location','code']]
要将 "code" 列合并到一个新列中,这会导致我想将一个值添加到第二个数据框中匹配的行。
df1['allcodes'] = df1[df1.columns[1:]].apply(lambda x: ','.join(x.dropna().astype(str)),axis=1)
现在 df1 看起来像:
terms code1 code2 code3 allcodes
0 term1 1234x 2345x 3456x 1234x,2345x,3456x
1 term2 4321y 5432y 6543y 4321y,5432y,6543y
我需要做的是,如果df2['code']在df1['allcodes']中,在df2中一行末尾添加对应的allcodes值,其中有一个匹配。
最终结果应该是:
name rate location code allcodes
0 Sara 3.5 OH 6543y 4321y,5432y,6543y
1 Conroy 5.2 NM 2345x 1234x,2345x,3456x
Dan 不应该在那里,因为他的代码不在 df1
我看过 merge/join/concat,但由于表格大小不同,而且 df2 中的代码可以出现在 df1 中的多个列中,所以我不知道如何使用这些函数。
这次是 lambda 函数,也许是 map?任何想法表示赞赏。
设置
df1
terms code1 code2 code3
0 term1 1234x 2345x 3456x
1 term2 4321y 5432y 6543y
df2
name rate location code
0 Dan 3 FL 4444g
1 Sara 3.5 OH 6543y
2 Conroy 5.2 NM 2345x
以 space 为代价,一种快速的方法是生成两个映射,然后链接两个 map
调用。
m1 = df1.melt('terms').drop('variable', 1).set_index('value').terms
m2 = df1.set_index('terms').apply(lambda x: \
','.join(x.values.ravel()), 1)
df2['allcodes'] = df2.code.map(m1).map(m2)
df2 = df2.dropna(subset=['allcodes'])
df2
name rate location code allcodes
1 Sara 3.5 OH 6543y 4321y,5432y,6543y
2 Conroy 5.2 NM 2345x 1234x,2345x,3456x
详情
m1
value
1234x term1
4321y term2
2345x term1
5432y term2
3456x term1
6543y term2
Name: terms, dtype: object
m2
terms
term1 1234x,2345x,3456x
term2 4321y,5432y,6543y
dtype: object
m1
会将 code
映射到 term
,而 m2
会将 term
映射到代码组。
简单的解决方案。
xx=df1.set_index('terms').values.tolist()
df2['New']=df2.code.apply(lambda x : [y for y in xx if x in y] )
df2=df2[df2.New.apply(len)>0]
df2['New']=df2.New.apply(pd.Series)[0].apply(lambda x : ','.join(x))
df2
Out[524]:
name rate location code New
1 Sara 3.5 OH 6543y 4321y,5432y,6543y
2 Conroy 5.2 NM 2345x 1234x,2345x,3456x
我需要 return df1 中一列的值,如果 df2 中的值在第一行,则将其附加到 df2 中的一行。
示例代码
df1 = pd.DataFrame(
{
'terms' : ['term1','term2'],
'code1': ['1234x', '4321y'],
'code2': ['2345x','5432y'],
'code3': ['3456x','6543y']
}
)
df1 = df1[['terms'] + df1.columns[:-1].tolist()]
df2 = pd.DataFrame(
{
'name': ['Dan','Sara','Conroy'],
'rate': ['3','3.5','5.2'],
'location': ['FL','OH','NM'],
'code': ['4444g','6543y','2345x']
})
df2 = df2[['name','rate','location','code']]
要将 "code" 列合并到一个新列中,这会导致我想将一个值添加到第二个数据框中匹配的行。
df1['allcodes'] = df1[df1.columns[1:]].apply(lambda x: ','.join(x.dropna().astype(str)),axis=1)
现在 df1 看起来像:
terms code1 code2 code3 allcodes
0 term1 1234x 2345x 3456x 1234x,2345x,3456x
1 term2 4321y 5432y 6543y 4321y,5432y,6543y
我需要做的是,如果df2['code']在df1['allcodes']中,在df2中一行末尾添加对应的allcodes值,其中有一个匹配。
最终结果应该是:
name rate location code allcodes
0 Sara 3.5 OH 6543y 4321y,5432y,6543y
1 Conroy 5.2 NM 2345x 1234x,2345x,3456x
Dan 不应该在那里,因为他的代码不在 df1
我看过 merge/join/concat,但由于表格大小不同,而且 df2 中的代码可以出现在 df1 中的多个列中,所以我不知道如何使用这些函数。
这次是 lambda 函数,也许是 map?任何想法表示赞赏。
设置
df1
terms code1 code2 code3
0 term1 1234x 2345x 3456x
1 term2 4321y 5432y 6543y
df2
name rate location code
0 Dan 3 FL 4444g
1 Sara 3.5 OH 6543y
2 Conroy 5.2 NM 2345x
以 space 为代价,一种快速的方法是生成两个映射,然后链接两个 map
调用。
m1 = df1.melt('terms').drop('variable', 1).set_index('value').terms
m2 = df1.set_index('terms').apply(lambda x: \
','.join(x.values.ravel()), 1)
df2['allcodes'] = df2.code.map(m1).map(m2)
df2 = df2.dropna(subset=['allcodes'])
df2
name rate location code allcodes
1 Sara 3.5 OH 6543y 4321y,5432y,6543y
2 Conroy 5.2 NM 2345x 1234x,2345x,3456x
详情
m1
value
1234x term1
4321y term2
2345x term1
5432y term2
3456x term1
6543y term2
Name: terms, dtype: object
m2
terms
term1 1234x,2345x,3456x
term2 4321y,5432y,6543y
dtype: object
m1
会将 code
映射到 term
,而 m2
会将 term
映射到代码组。
简单的解决方案。
xx=df1.set_index('terms').values.tolist()
df2['New']=df2.code.apply(lambda x : [y for y in xx if x in y] )
df2=df2[df2.New.apply(len)>0]
df2['New']=df2.New.apply(pd.Series)[0].apply(lambda x : ','.join(x))
df2
Out[524]:
name rate location code New
1 Sara 3.5 OH 6543y 4321y,5432y,6543y
2 Conroy 5.2 NM 2345x 1234x,2345x,3456x