Take some lists in a dataframe

How to take multiple lists and put them in different columns in a Python data frame? I tried this solution, but I am having problems.

Attempt 1:

  • Have three lists, and compress them together and use that res = zip(lst1,lst2,lst3)
  • Gives only one column

Attempt 2:

 percentile_list = pd.DataFrame({'lst1Tite' : [lst1], 'lst2Tite' : [lst2], 'lst3Tite' : [lst3] }, columns=['lst1Tite','lst1Tite', 'lst1Tite']) 
  • produces either one row per 3 columns (as indicated above), or if I rearrange, it is 3 rows and 1 column

How to get 100 rows (the length of each independent list) in 3 columns (three lists) of pandas dataframe?

+133
python numpy pandas
May 29 '15 at 6:37
source share
6 answers

I think that you are almost there, try to remove the extra square brackets around lst (you also do not need to specify column names when you create a data frame from such words):

 import pandas as pd lst1 = range(100) lst2 = range(100) lst3 = range(100) percentile_list = pd.DataFrame( {'lst1Title': lst1, 'lst2Title': lst2, 'lst3Title': lst3 }) percentile_list lst1Title lst2Title lst3Title 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 ... 

If you need a more productive solution, you can use np.column_stack rather than zip as in the first attempt, in this example it is about 2 times more acceleration, however, in my opinion, this requires cost of readability:

 import numpy as np percentile_list = pd.DataFrame(np.column_stack([lst1, lst2, lst3]), columns=['lst1Title', 'lst2Title', 'lst3Title']) 
+234
May 29 '15 at 6:40
source share

Adding to Aditya Guru here. No need to use a card. You can do it simply:

 pd.DataFrame(list(zip(lst1, lst2, lst3))) 

This will cause the column names to be 0,1,2. To set your own column names, you can pass the columns keyword argument to the method above.

 pd.DataFrame(list(zip(lst1, lst2, lst3)), columns=['lst1_title','lst2_title', 'lst3_title']) 
+39
Jun 16 '17 at 9:22 on
source share

Just adding that using the first approach this can be done as -

 pd.DataFrame(list(map(list, zip(lst1,lst2,lst3)))) 
+9
Feb 19 '17 at 18:44
source share

Adding another scalable solution.

 lists = [lst1, lst2, lst3, lst4] df = pd.concat([pd.Series(x) for x in lists], axis=1) 
+6
Jul 07 '18 at 8:18
source share

By adding the answers above, we can create on the fly

 df= pd.DataFrame() list1 = list(range(10)) list2 = list(range(10,20)) df['list1'] = list1 df['list2'] = list2 print(df) 

Hope it helps!

+4
Jan 16 '19 at
source share

@oopsi used pd.concat() but did not include column names. You could do the following, which, unlike the first solution in the accepted answer, gives you control over the order of the columns (avoiding differences that are unordered):

 import pandas as pd lst1 = range(100) lst2 = range(100) lst3 = range(100) s1=pd.Series(lst1,name='lst1Title') s2=pd.Series(lst2,name='lst2Title') s3=pd.Series(lst3,name='lst3Title') percentile_list = pd.concat([s1,s3,s3], axis=1) percentile_list Out[32]: lst1Title lst3Title lst3Title 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 7 7 7 7 8 8 8 8 ... 
0
Aug 17 '19 at 4:50
source share



All Articles