0

I have two dataframes:

db1 = pd.DataFrame(
    columns=["value", "type"], data=[[1, "A"],[2, "B"],[3, "C"],[4, "D"],[5, "E"]]
)
print(db1)

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

   value type
0      1    A
1      2    B
2      3    C
3      4    D
4      5    E
  value type
0   Nan    A
1   Nan    F

I just want to attribute value on the column "value" of the dataframe "db2" if value of db1.type equal to db2.type.

I have make an attempt, but I got an error:

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

ValueError: Can only compare identically-labeled Series objects

And I make another try:

db2['value'] = np.where(db1['type'].isin(db2['type']), db1["value"], np.nan)

ValueError: Length of values (9) does not match length of index (2)

Many thanks

Jen
  • 35
  • 6

0 Answers0