根据不同的数据框属性值

Attribute a value based on different dataframe

我有 2 个数据帧

db1 = pd.DataFrame(
    columns=["value", "type",'type2'], data=[[1, "A","ca"],[2, "B","cb"],[3, "C","cc"],[4, "D",'dd'],[5, "E",'ee'],["Nan","F",'ff']]
)
print(db1)

db2 = pd.DataFrame(
    columns=["value", "type"], data=[["Nan", "A"],["Nan", "F"]]
)

db1

    value   type    type2
0   1       A       ca
1   2       B       cb
2   3       C       cc
3   4       D       dd
4   5       E       ee
5   Nan     F       ff

db2

    value   type
0   Nan     A
1   Nan     F

我想在 db2 的新列中包含“type2”的值 if (db1['type'] == db2['type']) & (db1['value'] == db2['value'])

像那样:

db2

value   type    new
Nan     A   
Nan     F       ff

我尝试了一些但它不起作用

db2["new"] =np.nan

db2["new"]= np.where((db1['type'] == db2['type']) & (db1['value'] == db2['value']), db2['type2'], '')

谢谢!

试试这个(有点难看但有效:)

import pandas as pd 
db1 = pd.DataFrame(
    columns=["value", "type",'type2'], data=[[1, "A","ca"],[2, "B","cb"],[3, "C","cc"],[4, "D",'dd'],[5, "E",'ee'],["Nan","F",'ff']]
)
print(db1)

db2 = pd.DataFrame(
    columns=["value", "type"], data=[["Nan", "A"],["Nan", "F"]]
)
for index,row in db2.iterrows():
    type_=db2.loc[index,'type']
    value_=db2.loc[index,'value']
    
    df_tempo=db1.loc[(db1['type']==type_)&(db1['value']==value_),:]
    if df_tempo.shape[0]==1 : 
        db2.loc[index,'new']=df_tempo['type2'].values[0]
    else : 
        db2.loc[index,'new']='no_val'

输出:

db2
    value   type    new
0   nan     A      no_val
1   nan     F      ff
2   3       F      no_val

如果你只保留他们之间的匹配:

db2=db2.loc[db2['new']!='no_val',:]
db2
    value   type    new
1   Nan     F       ff

IIUC 你可以做一个合并,这应该会给你预期的结果

pd.merge(db1, db2, on = ['value', 'type'])

如果您想像示例中那样保留来自 db2 的数据,您可以添加一个 how = 'right' 以确保无论所有 db2 行都存在

pd.merge(db1, db2, on = ['value', 'type'], how = 'right')