我的熊猫数据框没有附加字典

时间:2021-07-24 15:50:48

标签: python pandas dataframe

我正在尝试用字典附加一个熊猫数据框。但是,它没有附加。打印时我得到一个空的数据框。请告诉我代码中哪里出错了。

import pandas as pd

dfs = pd.DataFrame()

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

1 个答案:

答案 0 :(得分:0)

试试:

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

调用 tstfunc() 时的输出:

    a       b
0   pavan   sunder

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

调用 tstfunc() 时的输出:

    a       b
0   pavan   sunder