Home > Back-end >  Edit pandas dataframe
Edit pandas dataframe

Time:12-27

I have a pandas dataframe output like this(it has 6 rows and each rows has a nested list data):

my output is like this

0 [[-127, -162, -197, -229, -245, -254, -261, -2...,]]

1 [[128, 157, 189, 226, 250, 257, 262, 265, 268,...,]]

2 [[56, 73, 85, 93, 100, 107, 113, 117, 118, 117...,]]

3 [[519, 619, 723, 827, 914, 956, 955, 934, 920,...,]]

4 [[-188, -239, -274, -316, -356, -374, -380, -3...,]]

5 [[-266, -316, -367, -407, -423, -423, -401, -3...,]]

6 [[21, 22, 24, 26, 28, 31, 32, 34, 34, 35, 35, ...,]]

And I want to unpack nested list and extract the data then convert datas to a pandas dataframe like this:

my desire output split and extract datas in nested list pandas dataframe and assign each data to a diffrent column

CodePudding user response:

I created a demo dataframe and then seperate data into multiple columns as you wanted

 import pandas as pd
data={0:[[[-127,-126,120,216,-118,-117]],[[-112,-113,-114,-112,-110,109]]]
     }
df=pd.DataFrame(data)
df.head()
l=len(df[0][0][0])
j=1
for p in df[0]:
    for q in p[0]:
        df['Col_' str(j)]=q
        j =1
df.drop(0,axis=1)

Final dataframe

Col_1   Col_2   Col_3   Col_4   Col_5   Col_6   Col_7   Col_8   Col_9   Col_10  Col_11  Col_12
0   -127    -126    120 216 -118    -117    -112    -113    -114    -112    -110    109
1   -127    -126    120 216 -118    -117    -112    -113    -114    -112    -110    109

CodePudding user response:

You can use the iterrows method

output_df = pd.DataFrame()
for r in df.iterrows():
    output_df = output_df.append(pd.DataFrame(r[1][0]), ignore_index=True)

CodePudding user response:

We can go with creating a new data frame, by selecting the values from list converting it into a dataframe and then rename the columns

Code:

converted_df = pd.DataFrame(df['val'].apply(lambda x:x[0]).to_list())
converted_df.columns = [f"Col_{col}" for col in converted_df.columns 1]
  • Related