添加行到dataframe语法问题

时间:2018-03-10 21:26:22

标签: python pandas dataframe

我正在尝试根据this post向数据框df添加新行,但我的语法错误。我错过了什么?

df = pd.DataFrame(columns=['poll1', 'poll2','tval','pval'])
temp = pd.DataFrame([0,0,1,2], columns=['poll1', 'poll2','tval','pval'])
df.append(temp)
print(df)

1 个答案:

答案 0 :(得分:0)

df = pd.DataFrame(columns=['poll1', 'poll2','tval','pval'], data=[[0,0,1,2]])
print(df)

或者如果你想使用追加:

cols = ['poll1', 'poll2','tval','pval']
df = pd.DataFrame(columns=cols)
temp = pd.DataFrame(data=[[0,0,1,2]], columns = cols)
df = df.append(temp)
print(df)