Pandas: how to set values in a dataframe specified by a loc (index and column) creating rows if index position doesn't exist?

Viewed 36

I have a dataframe with some "empty" (NaN) entries e.g.

c = pd.DataFrame({"A":[1, 2, np.NAN, 4], "B":[11, 22, 33, np.NAN]}, index=["a", "b", "c", "d"])

     A     B
a  1.0  11.0
b  2.0  22.0
c  NaN  33.0
d  4.0   NaN

that I want to "update" using two other dataframes containing values for column A and B respectively e.g.

a = pd.DataFrame({"A": [3, 4, 5, 6]}, index=["c", "d", "e", "f"])
b = pd.DataFrame({"B": [44, 55, 66]}, index=["d", "e", "f"])

to get an updated version:

     A     B
a  1.0  11.0
b  2.0  22.0
c  3.0  33.0
d  4.0  44.0
e  5.0  55.0
f  6.0  66.0

I tried using .loc

c.loc[a.index, "A"] = a and c.loc[a.index, "A"] = a.A

but keep getting a KeyError for the items not in the index. I also tried variants of concat but could not get the desired output.

Is there a pandas function that sets values if it already exists in the index and creates them if not?

1 Answers

Use two combine_first

df=b.combine_first(c).combine_first(a)
Out[302]: 
     A     B
a  1.0  11.0
b  2.0  22.0
c  3.0  33.0
d  4.0  44.0
e  5.0  55.0
f  6.0  66.0
Related