I'm trying to unpivot my data using pd.melt
but no success so far. Each row is a business, and the data contains information about the business and multiple reviews. I want my data to have every review as a row.
My first 150 columns are in groups of 15, each group column name shares the same pattern reviews/n/
for 0 < n < 9
. (reviews/0/text
, reviews/0/date
, ... , reviews/9/date
).
The next 65 columns in the dataframe include more data about the business (e.g. business_id
, address
) that should remain as id_variables.
My current data looks like this:
business_id | address | reviews/0/date | reviews/0/text | reviews/1/date | reviews/1/text |
---|---|---|---|---|---|
12345 | 01 street | 1/1/1990 | "abc" | 2/2/1995 | "def" |
and my new dataframe should have every review as a row instead of every business, and look like this:
business_id | address | review_number | review_date | review_text |
---|---|---|---|---|
12345 | 01 street | 0 | 1/1/1990 | "abc" |
12345 | 01 street | 1 | 2/2/1995 | "def" |
I tried using pd.melt
but could not succeed in making code that produced something valuable to me.