我的数据框(df_UP)如下所示:
Total_trends #up #down #flat
0.05 811 326 310 175
当我尝试使用以下代码附加一行时,我正在使用 jupyter 笔记本:
`d = {'Total_trends': [total.time], '#up': [good_trigger.time], '#down': [bad_trigger.time], '#flat': [flat.time]}
df_UP.append(d, ignore_index=True)`
第一次运行:
Total_trends #up #down #flat
0 811 326 310 175
1 [811] [326] [310] [175]
但是当我再次运行具有 d
中其他值的单元格时,它只是用新数据覆盖第 1 行的现有数据,知道为什么吗?谢谢!
最佳答案
您可以将字典转换为DataFrame
并分配回来:
d = {'Total_trends': [10], '#up': [20], '#down': [3], '#flat': [0]}
df_UP = df_UP.append(pd.DataFrame(d), ignore_index=True)
print (df_UP)
Total_trends #up #down #flat
0 811 326 310 175
1 10 20 3 0
d = {'Total_trends': [160], '#up': [270], '#down': [63], '#flat': [30]}
df_UP = df_UP.append(pd.DataFrame(d), ignore_index=True)
print (df_UP)
Total_trends #up #down #flat
0 811 326 310 175
1 10 20 3 0
2 160 270 63 30
关于python - Pandas 尝试向数据帧追加一行,但不断覆盖现有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54924265/