Convert pandas Series to DataFrame

前端 未结 6 1006
长情又很酷
长情又很酷 2020-11-27 11:08

I have a Pandas series sf:

email
email1@email.com    [1.0, 0.0, 0.0]
email2@email.com    [2.0, 0.0, 0.0]
email3@email.com    [1.0, 0.0, 0.0]
email4@email.com         


        
6条回答
  •  情话喂你
    2020-11-27 11:37

    to_frame():

    Starting with the following Series, df:

    email
    email1@email.com    A
    email2@email.com    B
    email3@email.com    C
    dtype: int64
    

    I use to_frame to convert the series to DataFrame:

    df = df.to_frame().reset_index()
    
        email               0
    0   email1@email.com    A
    1   email2@email.com    B
    2   email3@email.com    C
    3   email4@email.com    D
    

    Now all you need is to rename the column name and name the index column:

    df = df.rename(columns= {0: 'list'})
    df.index.name = 'index'
    

    Your DataFrame is ready for further analysis.

    Update: I just came across this link where the answers are surprisingly similar to mine here.

提交回复
热议问题