0

I was struggling with how to word the question, so I will provide an example of what I am trying to do below. I have a dataframe that looks like this:

     ID     CODE    COST
0   60086   V2401   105.38
1   60142   V2500   221.58
2   60086   V2500   105.38
3   60134   V2750   35
4   60134   V2020   0

I am trying to create a dataframe that has the ID as rows, the CODE as columns, and the COST as values since the cost for the same code is different per ID. How can I do this in?

twnsfan40
  • 41
  • 5
  • Does this answer your question? [How to pivot a dataframe](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) – r.ook Jun 05 '20 at 20:32

1 Answers1

1

This seems like a classic "long to wide" problem, and there are several ways to do it. You can try pivot, for example:

df.pivot_table(index='ID', columns='CODE', values='COST')

(assuming that the dataframe is df.)

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185