Python 需要将dic分配给数据帧

Python 需要将dic分配给数据帧,python,pandas,Python,Pandas,我在尝试将dict分配给df数据帧时遇到问题 df.loc[index,'count'] = dict() 当我收到此错误消息时: Incompatible indexer with Series 为了解决这个问题,我可以这样做 df.loc[index,'count'] = [dict()] ,但我不喜欢这个解决方案,因为我必须在获取字典之前解析列表,即 a = (df.loc[index,'count'])[0] 我如何才能以更优雅的方式解决这种情况 EDIT1 复制整个代码的一种方

我在尝试将dict分配给df数据帧时遇到问题

df.loc[index,'count'] = dict()
当我收到此错误消息时:

Incompatible indexer with Series
为了解决这个问题,我可以这样做

df.loc[index,'count'] = [dict()]
,但我不喜欢这个解决方案,因为我必须在获取字典之前解析列表,即

a = (df.loc[index,'count'])[0]
我如何才能以更优雅的方式解决这种情况

EDIT1

复制整个代码的一种方法如下

代码:

输出:

OK!
Traceback (most recent call last):

  File "<ipython-input-193-67bbd89f2c69>", line 4, in <module>
    df.loc[0, 'count'] = d

  File "/usr/lib64/python3.6/site-packages/pandas/core/indexing.py", line 194, in __setitem__
    self._setitem_with_indexer(indexer, value)

  File "/usr/lib64/python3.6/site-packages/pandas/core/indexing.py", line 625, in _setitem_with_indexer
    value = self._align_series(indexer, Series(value))

  File "/usr/lib64/python3.6/site-packages/pandas/core/indexing.py", line 765, in _align_series
    raise ValueError('Incompatible indexer with Series')

ValueError: Incompatible indexer with Series
好的!
回溯(最近一次呼叫最后一次):
文件“”,第4行,在
df.loc[0,'计数']=d
文件“/usr/lib64/python3.6/site packages/pandas/core/index.py”,第194行,在_setitem中__
self.\u setitem\u和索引器(索引器,值)
文件“/usr/lib64/python3.6/site packages/pandas/core/index.py”,第625行,在带有索引器的setitem中
值=自对齐系列(索引器,系列(值))
文件“/usr/lib64/python3.6/site packages/pandas/core/index.py”,第765行,在系列中
raise VALUERROR('与序列不兼容的索引器')
ValueError:与序列不兼容的索引器

尝试共享一些数据(或一个小示例),请参阅:更优雅的方式是不要在
数据框中包含列表或
目录。Pandas最适合矢量化数据及其操作,其中嵌套的数据结构与其用途相矛盾,并且会丢失许多功能。我建议您不要这样做。那么您想用字典中的数据替换数据框中的某些项吗?你能给出你正在使用的数据(用于数据帧和字典)吗?你能提供一个你想用虚拟数据实现什么的例子吗?通过你的例子,你可以做
df.loc[0]=[d,None]
,然后
df.loc[0,'count']
是一个
dict
,而不是一个
list
带有
dict
OK!
Traceback (most recent call last):

  File "<ipython-input-193-67bbd89f2c69>", line 4, in <module>
    df.loc[0, 'count'] = d

  File "/usr/lib64/python3.6/site-packages/pandas/core/indexing.py", line 194, in __setitem__
    self._setitem_with_indexer(indexer, value)

  File "/usr/lib64/python3.6/site-packages/pandas/core/indexing.py", line 625, in _setitem_with_indexer
    value = self._align_series(indexer, Series(value))

  File "/usr/lib64/python3.6/site-packages/pandas/core/indexing.py", line 765, in _align_series
    raise ValueError('Incompatible indexer with Series')

ValueError: Incompatible indexer with Series