Spread specific columns in dataframe without any aggregation?

Here is my toy df:

{'id': {0: 1089577, 1: 1089577, 2: 1089577, 3: 1089577, 4: 1089577},
 'title': {0: 'Hungarian Goulash Stew',
  1: 'Hungarian Goulash Stew',
  2: 'Hungarian Goulash Stew',
  3: 'Hungarian Goulash Stew',
  4: 'Hungarian Goulash Stew'},
 'readyInMinutes': {0: 120, 1: 120, 2: 120, 3: 120, 4: 120},
 'nutrients.amount': {0: 323.18, 1: 15.14, 2: 4.43, 3: 38.95, 4: 34.64},
 'nutrients.name': {0: 'Calories',
  1: 'Fat',
  2: 'Saturated Fat',
  3: 'Carbohydrates',
  4: 'Net Carbohydrates'},
 'nutrients.percentOfDailyNeeds': {0: 16.16,
  1: 23.3,
  2: 27.69,
  3: 12.98,
  4: 12.6},
 'nutrients.title': {0: 'Calories',
  1: 'Fat',
  2: 'Saturated Fat',
  3: 'Carbohydrates',
  4: 'Net Carbohydrates'},
 'nutrients.unit': {0: 'kcal', 1: 'g', 2: 'g', 3: 'g', 4: 'g'}}

I would like to spread the nutrients.title to be the columns. Sp I will get Fat, Saturated Fat ... columns with their corresponding values, without any agg.

What is the function that can do this without any aggregation? Just "reshaping".

I want it to be: enter image description here

How can I "spread" it like this?



Read more here: https://stackoverflow.com/questions/67388136/spread-specific-columns-in-dataframe-without-any-aggregation

Content Attribution

This content was originally published by SteveS at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: