I am trying to append a dictionary to my DataFrame.
This is how the DataFrame looks:
NR RS BP 0471 10 11.41 0652 10 11.50 0650 20 11.35 6519 40 11.06
And this is the dictionary:
bpDict = {"nr":["0471","0652","0650","6519"], "bp2":[11.39,11.47,nan,11.07], "bp3":[11.43,11.46,11.33,11.08], "bp4":[11.44,11.44,11.37,nan]}
I need to append this dictionary to my df. I know how to do it when I append one column (using map) but this is not going to work here.
Any ideas on how to append this?
Advertisement
Answer
You can use the merge()
method from Pandas dataframes. You can do something like that :
df = pd.DataFrame({ "nr":["0471","0652","0650","6519"], "RS":[10,10,20,40], "BP":[11.41, 11.50, 11.35, 11.06], }) bpDict = {"nr":["0471","0652","0650","6519"], "bp2":[11.39,11.47,11.38,11.07], "bp3":[11.43,11.46,11.33,11.08], "bp4":[11.44,11.44,11.37,11.11]} df_2 = pd.DataFrame(bpDict) df.merge(df_2, on="nr")