0

I am trying to append a pandas data frame with a dictionary. However, its not appending. I am getting an empty dataframe when I print it. Kindly tell me where have made a mistake in my code.

import pandas as pd

dfs = pd.DataFrame()

def tstfunc():
    dicts = {'a': "pavan", 'b':"sunder"}
    dfs.append(dicts, ignore_index=True)
    print(dfs)
    
tstfunc()
Pavan
  • 381
  • 1
  • 4
  • 19
  • From the accepted answer: "DataFrame.append is not an in-place operation" -> `dfs = dfs.append(dicts, ignore_index=True)` you might end up with a scoping issue, you'll also need to return the results back rather than printing since there are no inplace append, join, merge, or concatenation operations. – Henry Ecker Jul 24 '21 at 15:53

1 Answers1

0

Try:

def tstfunc():
    dfs = pd.DataFrame()
    dicts = {'a': "pavan", 'b':"sunder"}
    dfs=dfs.append(dicts, ignore_index=True)
    return dfs
    
tstfunc()

output when you call tstfunc():

    a       b
0   pavan   sunder

OR

dfs = pd.DataFrame()
def tstfunc():
    dicts = {'a': "pavan", 'b':"sunder"}
    return dfs.append(dicts, ignore_index=True)
    
tstfunc()

output when you call tstfunc():

    a       b
0   pavan   sunder
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41