0

Problem

Converting R to Python via Pandas, and I am trying to using pd.DataFrame.pivot to accomplish the following.

I am trying to turn the following table:

type life year_1 year_2 year_3 year_4 year_5 cost
A 4 0 22 24 24 315 0
B 1 35 35 35 35 35 1

into this other table

type life cost years units
A 4 0 year_one 0
A 4 0 year_two 22
A 4 0 year_three 24
A 4 0 year_four 24
A 4 0 year_five 315
B 1 1 year_one 35
B 1 1 year_two 35
B 1 1 year_three 35
B 1 1 year_four 35
B 1 1 year_five 35

The R code I am converting is the following:

datasheet %>% pivot_longer(cols = starts_with("year_"),
                           names_to = "year", 
                           values_to = "units")
M--
  • 25,431
  • 8
  • 61
  • 93

0 Answers0