Home > Software design >  Pandas Dataframe - Values are lists
Pandas Dataframe - Values are lists

Time:12-03

I have a Pandas Dataframe with one single column, but the values for each row are lists of five elements, something like that:

Column
timestamp
06:54:00 [1, 2, 3, 4, 5 ]
06:55:00 [0.5, 2.3, 4.5, 1, 3 ]

I would like to separate the data so I get another five columns, each of them containing one of the values of the list per row. Like this (I put only the two first ones to save space):

Column Column 1 Column 2
timestamp
06:54:00 [1, 2, 3, 4, 5 ] 1 2
06:55:00 [0.5, 2.3, 4.5, 1, 3 ] 0.5 2.3

I tried with:

        L = [pd.DataFrame(data[col].values.tolist()) for col in data]
        print(L)
        df_new = pd.concat(L, axis=1, ignore_index=True)
        print(df_new)

And

        for column in data.columns:
            column_name = f'TColumn {column}'
            val = data[column][column]
            n = 0
            for n in range(5):
                data[column_name] = val[n]
                n = n   1
        print(data)

I haven't managed to get anything, could someone please give me a hand with this?

Thank you in advance,

CodePudding user response:

To further simplify what @Manlai A has posted, we can create new columns on-the-fly like this:

df[[f'Column {i}' for i in range(5)]] = df['Columns'].tolist()

And yes, this oneliner actually answer the question above.

CodePudding user response:

You must first create the columns and then convert df['Column'].values to a list and assign it to the newly created columns. You can do:

for i in range(1,6):
    df['Column ' str(i)] = np.nan
df.loc[:,'Column 1':'Column 5'] = df['Column'].tolist()
  • Related