0

I have seen a variety of answers to this question (like this one), and have had no success in getting my lists into one dataframe. I have one header list (meant to be column headers), and then a variable that has multiple records in it:

list1 = ['Rank', 'Athlete', 'Distance', 'Runs', 'Longest', 'Avg. Pace', 'Elev. Gain']

list2 = (['1', 'Jack', '57.4 km', '4', '21.7 km', '5:57 /km', '994 m']
['2', 'Jill', '34.0 km', '2', '17.9 km', '5:27 /km', '152 m']
['3', 'Kelsey', '32.6 km', '2', '21.3 km', '5:46 /km', '141 m'])

When I try something like:

df = pd.DataFrame(list(zip(['1', 'Jack, '57.4 km', '4', '21.7 km', '5:57 /km', '994 m'],
#                                      ['2', 'Jill', '34.0 km', '2', '17.9 km', '5:27 /km', '152 m'])))

It lists all the attributes as their own rows, like so:

    0   1
0   1   2
1   Jack    Jill
2   57.4 km 34.0 km
3   4   2
4   21.7 km 17.9 km
5   5:57 /km    5:27 /km
6   994 m   152 m

How do I get this into a frame that has list1 as the headers, and the rest of the data neatly squared away?

papelr
  • 468
  • 1
  • 11
  • 42

2 Answers2

1

Given

list1 = ['Rank', 'Athlete', 'Distance', 'Runs', 'Longest', 'Avg. Pace', 'Elev. Gain']
list2 = (['1', 'Jack', '57.4 km', '4', '21.7 km', '5:57 /km', '994 m'],
    ['2', 'Jill', '34.0 km', '2', '17.9 km', '5:27 /km', '152 m'],
    ['3', 'Kelsey', '32.6 km', '2', '21.3 km', '5:46 /km', '141 m'])

do

pd.DataFrame(list2, columns=list1)

which returns

  Rank Athlete Distance Runs  Longest Avg. Pace Elev. Gain
0    1    Jack  57.4 km    4  21.7 km  5:57 /km      994 m
1    2    Jill  34.0 km    2  17.9 km  5:27 /km      152 m
2    3  Kelsey  32.6 km    2  21.3 km  5:46 /km      141 m
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
1

Change your second list into a list of lists and then df = pd.DataFrame(columns = list1, data = list2)

umbreon29
  • 223
  • 2
  • 8
  • What's the best way to change something into a lists of lists? – papelr May 03 '20 at 03:23
  • Your list2 should look like `list2 = [['1', 'Jack', '57.4 km', '4', '21.7 km', '5:57 /km', '994 m'], ['2', 'Jill', '34.0 km', '2', '17.9 km', '5:27 /km', '152 m'], ['3', 'Kelsey', '32.6 km', '2', '21.3 km', '5:46 /km', '141 m']]` – umbreon29 May 03 '20 at 03:34
  • I threw this in there: `i=0 new_list=[] while i – papelr May 03 '20 at 03:35