2

Hi I want to append dictionary to DataFrame, but in this dictionary I dont have any index value. I also need to remove everything except "1393" from 'KP' value. This looks like this:

To this df:enter image description here

I want to append dictionary like this:

dict = {'KP': [1.0    1393
  Name: KP, dtype: int64],
 'Wiek': [Timedelta('176 days 12:59:43.042156102')],
 'KY1': [113.0],
 'OKO': [57.51],
 'GS': [10.59],
 'T': [654.31],
 'MP': [58.9]}

I try this:

df.append(dict,ignore_index=True)

But nothing happend, df is still the same and i dont get any error. I try to convert this dictionary to list and append list as row to df but I got the same result.

Ok i try this:

df = df.append(dict, ignore_index = True)

I get this output: enter image description here

This is not good enought i need clear data.

Park
  • 2,446
  • 1
  • 16
  • 25
maciej.o
  • 127
  • 1
  • 13
  • 2
    Use `df = df.append(dict, ignore_index = True)`. – Vishal A. Jan 28 '22 at 08:07
  • 3
    You can convert the dictionary into a dataframe: `df1 = pd.DataFrame(dict)` and then append: `df = df.append(df1, ignore_index = True)`. Btw using `dict` as a variable name is highly non-recomended as dict is a built-in keyword; also please never post images of dataframes, post them as text as explained in here: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – piterbarg Jan 28 '22 at 08:51
  • Ok thanks that realy help me – maciej.o Jan 28 '22 at 10:01

1 Answers1

3

As the 'append' method is deprecated, you may need to consider to use 'concat':

the_dict = {'k1':'v1','k2':'v2'}
df_the_dict = pd.DataFrame({'name':the_dict.keys(), 'value':the_dict.values()})

df_final = pd.concat([df_final, df_the_dict], ignore_index=True)
Mark K
  • 8,767
  • 14
  • 58
  • 118
  • 1
    I find adding rows to Pandas unbelievably unelegant un unpythonic. I cannot believe that the append method was dropped, which was intuitive and which would accept dictionaries. – Roel Verhoeven Jul 11 '23 at 18:26