我在Python中有一个具有不同键的字典列表(所有的键都有响应键),我希望将它转换为Panda,这样所有的键值都显示为行,Response键值作为一列重复。例如,以下列表
[ {'Response':1,‘工作生活’:5,‘生活家庭’:2,‘家庭平衡’:10},{'Response':2,‘鼓励管理’:11,‘管理职业’:1,‘职业抱负’:4,‘抱负发展’:8},{'Response':3,‘鼓励人民’:15,‘人经理’:9,‘经理问’5,‘询问员工’:9} ]
应该成为3列数据格式。
Response | Attribute | Value
1, work life, 5
1, life family, 2
1, family balance, 10
2, encouragement management, 11
2, management career, 1
2, career aspirations, 4
2, aspirations develop, 8
3, encourage people, 15
3, people managers, 9
3, managers ask, 5
3, ask employees, 9发布于 2017-11-15 18:11:13
这可能就是你要找的。使用stack,然后重置索引和列名。
df = pd.DataFrame(d).set_index('Response').stack().reset_index()
df.columns = ['Response', 'Attribute', 'Value']
df
Response Attribute Value
0 1 family balance 10.0
1 1 life family 2.0
2 1 work life 5.0
3 2 aspirations develop 8.0
4 2 career aspirations 4.0
5 2 encouragement management 11.0
6 2 management career 1.0
7 3 ask employees 9.0
8 3 encourage people 15.0
9 3 managers ask 5.0
10 3 people managers 9.0d是您的字典数据。请记住,字典不是有序的,所以总是期望与你的问题相同的顺序是不合理的。
https://stackoverflow.com/questions/47314382
复制相似问题