Home Forums Main Forums Python Forum Convert a list to a data frame in Python

  • Convert a list to a data frame in Python

     Justin updated 3 years, 6 months ago 1 Member · 1 Post
  • Justin

    Administrator
    October 25, 2020 at 10:49 pm
    Up
    0
    Down

    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)

    • This discussion was modified 3 years, 6 months ago by  Justin.
    • This discussion was modified 3 years, 6 months ago by  Justin.

Log in to reply.

Original Post
0 of 0 posts June 2018
Now