我有一个名为table的df,我想插入一个0的列表作为第一行。
table:
A B C D
Date
2016-04-21 0.020068 0.027631 0.006116 0.005451
2016-04-22 0.008741 0.006732 -0.013585 0.003863
2016-04-25 -0.020516 -0.020339 -0.007526 -0.010102
2016-04-26 0.051630 0.032790 0.008437 0.015524
我做了一个函数,没有创建一个0的长度=标题数的列表。
def arr():
arr = []
for i in range(len(list(table()))):
arr.append(0)
return arr
pd.DataFrame([arr()],columns=list(table()))
print(table())
给出错误:
Shape of passed values is (1, 1), indices imply (4, 1)
答案 0 :(得分:1)
首先需要按df
和add new row创建新的索引值sort_index
至loc
:
df.loc[df.index[0] - pd.offsets.DateOffset(days=1)] = 0
df = df.sort_index()
print (df)
A B C D
Date
2016-04-20 0.000000 0.000000 0.000000 0.000000
2016-04-21 0.020068 0.027631 0.006116 0.005451
2016-04-22 0.008741 0.006732 -0.013585 0.003863
2016-04-25 -0.020516 -0.020339 -0.007526 -0.010102
2016-04-26 0.051630 0.032790 0.008437 0.015524