I have a dictionary that contains a key and a list of values. I want to make a simple pandas df, however, I am having trouble breaking out the list items into separate rows. My current approach creates a df but keeps the values in a list. How can I pull them out and add them as separate rows?
d = {'Car':['1','2','3'],
'Chicken legs':['4','5','6'],
'Coronary artery bypass graft surgery':['7','8','9','10','11','12']}
df = pd.DataFrame({'Thing':list(d.keys()), 'Values':list(d.values())})
Thing Values
0 Car [1, 2, 3]
1 Chicken_legs [4, 5, 6]
2 Coronary artery bypass graft surgery [7, 8, 9, 10, 11, 12]
Anticipated end result:
Thing Values
0 Car 1
1 Car 2
2 Car 3
3 Chicken_legs 4
4 Chicken_legs 5
5 Chicken_legs 6
6 Coronary artery bypass graft surgery 7
7 Coronary artery bypass graft surgery 8
8 Coronary artery bypass graft surgery 9
9 Coronary artery bypass graft surgery 10
10 Coronary artery bypass graft surgery 11
11 Coronary artery bypass graft surgery 12
Just munge your dictionary into the right thing:
In [3]: pd.DataFrame({'Thing':k, 'Values':v} for k,V in d.items() for v in V)
Out[3]:
Thing Values
0 Coronary artery bypass graft surgery 7
1 Coronary artery bypass graft surgery 8
2 Coronary artery bypass graft surgery 9
3 Coronary artery bypass graft surgery 10
4 Coronary artery bypass graft surgery 11
5 Coronary artery bypass graft surgery 12
6 Car 1
7 Car 2
8 Car 3
9 Chicken legs 4
10 Chicken legs 5
11 Chicken legs 6
This solution is inferior to the accepted one by juanpa.arrivillaga.
pd.concat([ pd.DataFrame({'Thing': df['Thing'][i], 'Values': pd.Series(df.Values[i])}) for i in range(len(df)) ])
Thing Values
0 Car 1
1 Car 2
2 Car 3
0 Chicken legs 4
1 Chicken legs 5
2 Chicken legs 6
0 Coronary artery bypass graft surgery 7
1 Coronary artery bypass graft surgery 8
2 Coronary artery bypass graft surgery 9
3 Coronary artery bypass graft surgery 10
4 Coronary artery bypass graft surgery 11
5 Coronary artery bypass graft surgery 12
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.