我有一个熊猫数据框柱看起来有点像:大熊猫含系列阵列
Out[67]:
0 ["cheese", "milk...
1 ["yogurt", "cheese...
2 ["cheese", "cream"...
3 ["milk", "cheese"...
现在,最终我想这是一个平坦的列表,但在试图拉平这个,我注意到,大熊猫对待["cheese", "milk", "cream"]
作为str
而非list
我将如何去压扁这使我结束了:
["cheese", "milk", "yogurt", "cheese", "cheese"...]
[编辑] 所以下面给出的答案似乎是:
s = pd.Series(["['cheese', 'milk']", "['yogurt', 'cheese']", "['cheese', 'cream']"])
s = s.str.strip("[]")
df = s.str.split(',', expand=True)
df = df.applymap(lambda x: x.replace("'", '').strip())
l = df.values.flatten()
print (l.tolist())
这是伟大的,问题解答,答案接受,但它给我的印象相当不雅的解决方案。
的可能的复制[蟒蛇大熊猫展平数据帧到列表(http://stackoverflow.com/questions/25440008/python- pandas-flatten-a-dataframe-to-a-list) – soon
不,它不是重复的,因为列的类型是字符串而不是列表 – jezrael