0

In Pandas, I'm trying to relocate data placed in several columns, to several rows in the sense suggested by the two tables below.

Example

Each customer should appear in as many rows as they made purchases. If it matters, column j+1 can only have a value if column j does too.

How do I go about this?

smci
  • 32,567
  • 20
  • 113
  • 146
Git Gud
  • 217
  • 3
  • 10
  • 1
    Try to share data and not images. – Anton vBR Jan 23 '18 at 18:41
  • This is called [**converting from wide-form to long-form**](https://stackoverflow.com/search?q=%5Bpython%5D+long+form+wide+form) (or "melting"). There are many duplicates already, please pick one of those 49 hits and close-as-duplicate. – smci Jan 23 '18 at 18:46
  • @smci Thank you very much for the terminology. That's useful. I will do as you suggest. – Git Gud Jan 23 '18 at 18:47

1 Answers1

1

Pandas offers something specifically meant for this called melt: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html

Without a sample of your code I can't give an example that is code specific, but you would have something like this:

id_vars = ['Customer']
var_name = 'Purchase Type'
value_name = 'Value'

melted_df = pf.melt(unmelted_df, id_vars=id_vars, var_name=var_name, value_name=value_name)

As a result, you would get a melted DataFrame where Purchase 1, Purchase 2, etc are attributes of 'Purchase Type' and the value of those original purchases is displayed in Value.

Brandon Barney
  • 2,382
  • 1
  • 9
  • 18