这个问题在这里已经有答案了:
Python将列表插入单元格
(9个答案)
(9个答案)
五年前关闭的
我想将单元格值设置为列表。比如说:
df.loc['a']['b']=['1','2','3']
但是,由于出现以下错误,我无法执行此操作:
ValueError:使用iterable设置时,必须具有相等的len键和值
我的数据帧目前只是全零,是nxn。是否有任何方法可以设置单元格值,以便当我执行df.loc['a']['b']
时,返回['one','two','three']
问题是,您可能有一个数据帧,其中所有列都是float或int类型的系列。解决方案是将它们更改为“object”类型
[3]on
:df=pd.DataFrame(np.zero((4,4)))
In[4]:df
Output[4]:
0 1 2 3
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0
At[5]on:df.dtypes
Output[5]:
0.64
1.64
2.64
3.64
数据类型:对象
At[6]on:df=df.astype('object')
[7]on:df[1][2]=[1,2,3]
In[8]:df
Output[8]:
0 1 2 3
0 0 0 0 0
1 0 0 0 0
2 0 [1, 2, 3] 0 0
3 0 0 0 0