我有一個熊貓數據框柱看起來有點像:大熊貓含系列陣列
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