I would like convert each list column into one single column and then concat for below dataframe
data = {'labels': ['[management,workload,credibility]','[ethic,hardworking,profession]'],
'Score': [[0.55,0.36,0.75],[0.41,0.23,0.14]]}
# Create DataFrame
df = pd.DataFrame(data)
new dataframe output should look like this
labels Score
management 0.55
workload 0.36
credibility 0.75
ethic 0.41
hardworking 0.23
profession 0.14
Thank you
CodePudding user response:
Create lists from labels
column by Series.str.strip
and Series.str.split
and then use DataFrame.explode
:
df1 = (df.assign(labels= df['labels'].str.strip('[]').str.split(','))
.explode(['labels','Score']))
For oldier pandas versions use:
df1 = (df.assign(labels= df['labels'].str.strip('[]').str.split(','))
.apply(lambda x: x.explode()))
print (df1)
labels Score
0 management 0.55
0 workload 0.36
0 credibility 0.75
1 ethic 0.41
1 hardworking 0.23
1 profession 0.14