Split strings in tuples into columns, in Pandas

前端 未结 3 2111
梦如初夏
梦如初夏 2020-12-03 02:43

I have the following DataFrame, where Track ID is the row index. How can I split the string in the stats column into 5 columns of numb

3条回答
  •  独厮守ぢ
    2020-12-03 03:18

    If you have a sequence of tuples and not strings, and you want them as DataFrame columns, this is the simplest approach:

    df = pd.concat([df['Track ID'],pd.DataFrame(df['stats'].values.tolist())], axis=1)
    

    If it is actually strings, you can first convert it to lists like so, then apply the above operation:

    dfpart = pd.DataFrame(df['stats'].apply(lambda x: x.strip('()').split(', ')).values.tolist()).astype(float)
    df = pd.concat([df['Track ID'], dfpart], axis=1)
    

提交回复
热议问题