jfalkson jfalkson - 2 months ago 6
Python Question

Take multiple lists into dataframe

How do I take multiple lists and put them as different columns in a python dataframe? I tried following Read lists into columns of pandas DataFrame but had some trouble.

Attempt 1:


  • Have three lists, and zip them together and use that res = zip(lst1,lst2,lst3)

  • Yields just one column



Attempt 2:

percentile_list = pd.DataFrame({'lst1Tite' : [lst1],
'lst2Tite' : [lst2],
'lst3Tite':[lst3]
}, columns=['lst1Tite','lst1Tite', 'lst1Tite'])


- yields either one row by 3 columns (the way above) or if I transpose it is 3 rows and 1 column

How do I get a 100 row (length of each independent list) by 3 column (three lists) pandas dataframe?

Answer

I think you're almost there, try removing the extra square brackets around the lst's (Also you don't need to specify the column names when you're creating a dataframe from a dict like this):

lst1 = range(100)
lst2 = range(100)
lst3 = range(100)
percentile_list = pd.DataFrame({'lst1Tite' : lst1,
 'lst2Tite' : lst2,
 'lst3Tite':lst3
  })

percentile_list
    lst1Tite  lst2Tite  lst3Tite
0          0         0         0
1          1         1         1
2          2         2         2
3          3         3         3
4          4         4         4
5          5         5         5
6          6         6         6
...