Home › Forums › Main Forums › Python Forum › Convert a list to a data frame in Python
-
Convert a list to a data frame in Python
-
A very useful skill from below source:
https://datatofish.com/list-to-dataframe/
from pandas import DataFrame
### convert a list to a dataframe People_List = ['Jon','Mark','Maria','Jill','Jack'] df = DataFrame (People_List, columns=['First_Name']) print (df)### Convert List of Lists
People_List = [['Jon','Smith',21],
['Mark','Brown',38],
['Maria','Lee',42],
['Jill','Jones',28],
['Jack','Ford',55] ]df = DataFrame (People_List, columns=['First_Name','Last_Name', 'Age']) print print(df)
It gives a 5 X 3 data frame.
An alternative way is to use transpose() method as shown below, which produces identical results.
### transpose() method. ##################
People_List = [['Jon','Mark','Maria','Jill','Jack'],
['Smith','Brown','Lee','Jones','Ford'],
[21,38,42,28,55] ]
df = DataFrame (People_List).transpose() df.columns = ['First_Name', 'Last_Name', 'Age'] print (df)
Log in to reply.