1

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
CandleWax
  • 2,159
  • 2
  • 28
  • 46
  • https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_dict.html – PEZO Apr 24 '18 at 23:53
  • Related: [Convert Python list to pandas Dataframe/Series](https://stackoverflow.com/questions/21646791/convert-python-list-to-pandas-dataframe-series) – smci Apr 25 '18 at 00:18
  • Also, this is similar to a dataframe wide-to-long form conversion, but not exactly, since what would be multiple 'columns' are here stored in a list inside a single column. – smci Apr 25 '18 at 00:19

2 Answers2

4

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
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
1

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
smci
  • 32,567
  • 20
  • 113
  • 146