Home > database >  dict_keys & dict_values append to columns in existing dataframe
dict_keys & dict_values append to columns in existing dataframe

Time:11-27

i got a dict with keys and values e.g.:

dict_1 = {'Dog': ['Dog'], 'Car': ['Cat']}

and a DataFrame

,name,values
0   Apple   [Apple]
1   Pear    [Pear]
2   Lemon   [Lemon]

i would like to add /append the dict to the columns in an existing dataframe

,name,values
0   Apple   [Apple]
1   Pear    [Pear]
2   Lemon   [Lemon]
3   Dog     [Dog]
4   Cat     [Cat]

I am desperate at this seemingly simple task. Could you please help me?

CodePudding user response:

Maybe you can try something like this:

for k, v in dict_1.items():
    temp = pd.DataFrame({"names": k, "values": [v]})
    df = df.append(temp)
  • Related