Convert List to Pandas Dataframe Column
Use:
L = ['Thanks You', 'Its fine no problem', 'Are you sure']#create new df df = pd.DataFrame({'col':L})print (df) col0 Thanks You1 Its fine no problem2 Are you sure
df = pd.DataFrame({'oldcol':[1,2,3]})#add column to existing df df['col'] = Lprint (df) oldcol col0 1 Thanks You1 2 Its fine no problem2 3 Are you sure
Thank you DYZ:
#default column name 0df = pd.DataFrame(L)print (df) 00 Thanks You1 Its fine no problem2 Are you sure
if your list looks like this: [1,2,3] you can do:
import pandas as pdlst = [1,2,3]df = pd.DataFrame([lst])df.columns =['col1','col2','col3']df
to get this:
col1 col2 col30 1 2 3
alternatively you can create a column as follows:
import numpy as npimport pandas as pddf = pd.DataFrame(np.array([lst]).T)df.columns =['col1']df
to get this:
col10 11 22 3
You can directly call the
method and pass your list as parameter.
l = ['Thanks You','Its fine no problem','Are you sure']pd.DataFrame(l)
Output:
00 Thanks You1 Its fine no problem2 Are you sure
And if you have multiple lists and you want to make a dataframe out of it.You can do it as following:
import pandas as pdnames =["A","B","C","D"]salary =[50000,90000,41000,62000]age = [24,24,23,25]data = pd.DataFrame([names,salary,age]) #Each list would be added as a rowdata = data.transpose() #To Transpose and make each rows as columnsdata.columns=['Names','Salary','Age'] #Rename the columnsdata.head()
Output:
Names Salary Age0 A 50000 241 B 90000 242 C 41000 233 D 62000 25