我有这样的数据框df
:
date item
2019-03-29 [book,pencil]
...
我想使用循环获取列表中的每个项目。这是我尝试过的:
for i in range(len(df)):
for x in df['attributeName'][i]:
print(x)
但是我得到了每一个角色。所需的输出是:
book
pencil
如何解决这个问题?
最佳答案
df2['item'].apply(pd.Series).unstack().reset_index(drop=True)
更好的选择.. A Common Pitfall: Exploding Columns of Lists
pd.DataFrame(df2['item'].tolist()).unstack().reset_index(drop=True)
输出
0 book
1 pencil
dtype: object
关于python - 使用循环获取数据框列中的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56830117/