Skip to content
Advertisement

Transpose dataframe based on column list

I have a dataframe in the following structure:

JavaScript

I would like to transpose – create columns from the names in cNames.
But I can’t manage to achieve this with transpose because I want a column for each value in the list.
The needed output:

JavaScript

How can I achieve this result?
Thanks!

The code to create the DF:

JavaScript

Advertisement

Answer

One option is concat:

JavaScript

Or a DataFrame construction:

JavaScript

Output:

JavaScript

Update Performances sort by run time on sample data

DataFrame

JavaScript

concat:

JavaScript

KJDII’s new series

JavaScript

Scott’s apply(pd.Series.explode)

JavaScript

wwnde’s set_index.apply(explode)

JavaScript

Celius’ double explode

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement