问题描述
具有如下数据框:
df_data=pd.DataFrame({'name':[['ABC','DOS','TRES'],['XYZ','MORTGAGE','SOLUTIONS']],
'original': ['ABC DOS TRES','XYZ MORTGAGE SOLUTIONS']})
我正在使用chain.from_iterable提取列表中的每个项目并将结果添加到数据框中:
I am using chain.from_iterable to extract every item in a list and add the result to a dataframe:
s = pd.DataFrame(chain.from_iterable(df_data['name']),columns=['word'])
我该怎么做?
t = pd.DataFrame({'word': chain.from_iterable(df_data['name'])})
最后一次创建数据框时出现错误TypeError: object of type 'itertools.chain' has no len()
.两种数据框创建之间有什么区别?如何解决上一次创建中的错误?
The last creation of dataframe is giving an error TypeError: object of type 'itertools.chain' has no len()
. What is the difference between both dataframe creations? How the error in the last creation can be fixed?
谢谢:)
推荐答案
使用chain.from_iterable
返回一个迭代器,而不是列表/序列.较早版本的Pandas需要传递给数据框构造函数的对象具有len
,因此它知道要在后端分配的大小数组. chain
对象不提供(也不应该提供).
Using chain.from_iterable
returns an iterator, not a list/sequence. Older versions of Pandas needs the objects you pass to the data frame constructor to have a len
so it knows what size array to allocate on the backend. The chain
object does not supply that (nor should it).
您可以将其包装在list
中,以解决您的问题:
You can wrap it in list
is solve your issue:
t = pd.DataFrame({'word': list(chain.from_iterable(df_data['name']))})
这篇关于Pandas chain.from_iterable:类型为'itertools.chain'的错误对象没有len()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!