更新时间:2023-12-02 23:48:46
如果将列表转换为Series,那么它将正常工作:
If you convert the list to a Series then it will just work:
datasetTest.loc[:,'predict_close'] = pd.Series(test_pred_list)
示例:
In[121]:
df = pd.DataFrame({'a':np.arange(3)})
df
Out[121]:
a
0 0
1 1
2 2
In[122]:
df.loc[:,'b'] = pd.Series(['a','b'])
df
Out[122]:
a b
0 0 a
1 1 b
2 2 NaN
文档将其称为通过放大设置讨论添加或扩展,但也可以在长度小于预先存在的索引的情况下使用.
The docs refer to this as setting with enlargement which talks about adding or expanding but it also works where the length is less than the pre-existing index.
要处理索引不是从0
开始还是实际上不是int的地方:
To handle where the index doesn't start at 0
or in fact is not an int:
In[126]:
df = pd.DataFrame({'a':np.arange(3)}, index=np.arange(3,6))
df
Out[126]:
a
3 0
4 1
5 2
In[127]:
s = pd.Series(['a','b'])
s.index = df.index[:len(s)]
s
Out[127]:
3 a
4 b
dtype: object
In[128]:
df.loc[:,'b'] = s
df
Out[128]:
a b
3 0 a
4 1 b
5 2 NaN
如果希望致电fillna