Convert List to Pandas Dataframe Column

Inherited Geek picture Inherited Geek · Feb 5, 2017 · Viewed 253.6k times · Source

I need to Convert my list into a one column pandas dataframe

Current List (len=3):

['Thanks You',
 'Its fine no problem',
 'Are you sure']

Required Pandas DF (shape =3,):

0 Thank You
1 Its fine no problem
2 Are you sure

Please note the numbers represent index in Required Pandas DF above.

Answer

jezrael picture jezrael · Feb 5, 2017

Use:

L = ['Thanks You', 'Its fine no problem', 'Are you sure']

#create new df 
df = pd.DataFrame({'col':L})
print (df)

                   col
0           Thanks You
1  Its fine no problem
2         Are you sure

df = pd.DataFrame({'oldcol':[1,2,3]})

#add column to existing df 
df['col'] = L
print (df)
   oldcol                  col
0       1           Thanks You
1       2  Its fine no problem
2       3         Are you sure

Thank you DYZ:

#default column name 0
df = pd.DataFrame(L)
print (df)
                     0
0           Thanks You
1  Its fine no problem
2         Are you sure