Home > database >  Over writing a dataframe with a list of dicts
Over writing a dataframe with a list of dicts

Time:05-26

Suppose a dict like

rows = [{"A":1, "B":2},{"A":2, "B":2},{"A":3, "B":2}]

A dataframe df may or may not contain A and B columns, in either case I want to concat or update its corresponding values.

My solution is:

data = rows[0]

for key,val in data.items():
    if key in df:
       df.drop(key, axis=1)

df2 = pd.DataFrame(rows)
df = pd.concat([df, df2], axis=1)

I would like to know if it is correct and safe or if there are better ways.

Suppose df is:

   A  C
   10 4
   10 4
   10 4

After update with rows it should be:

   A   B   C
   1   2   4
   2   2   4
   3   2   4

In other words the data frame is over written with new values. Maybe combine function is useful, however it wants a function.

CodePudding user response:

Are you trying to merge the 2 DataFrame to fill in the missing column?

df1 = pd.DataFrame([{"A":1, "B":2},{"A":3, "B":2},{"A":4, "B":3}])
dfA = df1.drop("B", axis=1)

print(pd.concat([df1, dfA], axis=1))
   A  B  A
0  1  2  1
1  3  2  3
2  4  3  4

print(pd.merge(df1, dfA, on="A", how="left"))
   A  B
0  1  2
1  3  2
2  4  3

CodePudding user response:

To update a pandas dataframe you can use the append method. for instance, dataframe df needs to be updated.

rows=([{'A':5,},{'A':0,'B':9},{'A':7,'B':25}]
df=pd.DataFrame(rows)

new_rows=[{"A":1, "B":2},{"A":3, "B":2},{"A":4, "B":3}]

df.append(new_rows,ignore_index=True) # ignore index to reset the numbering
  • Related