Convert List to Pandas Dataframe Column

前端 未结 5 1479
别跟我提以往
别跟我提以往 2020-12-02 05:34

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\']
         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 06:14

    You can directly call the

    pd.DataFrame()

    method and pass your list as parameter.

    l = ['Thanks You','Its fine no problem','Are you sure']
    pd.DataFrame(l)
    

    Output:

                          0
    0            Thanks You
    1   Its fine no problem
    2          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 pd
    names =["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 row
    data = data.transpose() #To Transpose and make each rows as columns
    data.columns=['Names','Salary','Age'] #Rename the columns
    data.head()
    

    Output:

        Names   Salary  Age
    0       A    50000   24
    1       B    90000   24
    2       C    41000   23
    3       D    62000   25
    

提交回复
热议问题