I have a pandas dataframe with time series data, where the columns are looking like this:
| Customer | Item | Date | 00:00 | 00:30 | 01:00 | ... | 23:30 |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| XYZ | A | 2020-01-01 | 0 | 1 | 2 | ... | 3 |
| XYZ | B | 2020-01-02 | 0 | 2 | 2 | ... | 5 |
| ABC | A | 2020-01-01 | 0 | 1 | 5 | ... | 3 |
| ABC | B | 2020-01-02 | 0 | 2 | 2 | ... | 1 |
So the hours are in the columns, instead of the rows. I want to manipulate this dataframe, concatenate the time columns into the date column, and make them a separate rows, like this:
| Customer | Date | Item A | Item B |
| ------------ | ------------ | ------------ | ------------ |
| XYZ | 2020-01-01 00:00 | 1 | 2 |
| XYZ | 2020-01-01 00:30 | 1 | 2 |
| XYZ | 2020-01-01 01:00 | 1 | 2 |
| XYZ | 2020-01-02 00:00 | 1 | 2 |
| XYZ | 2020-01-02 00:30 | 1 | 2 |
| XYZ | 2020-01-02 01:00 | 1 | 2 |
| ABC | 2020-01-01 00:00 | 2 | 3 |
| ABC | 2020-01-01 00:30 | 2 | 2 |
| ABC | 2020-01-01 01:00 | 4 | 2 |
| ABC | 2020-01-02 00:00 | 2 | 3 |
| ABC | 2020-01-02 00:30 | 2 | 2 |
| ABC | 2020-01-02 01:00 | 4 | 2 |
How can I do this? I tried a method using cross join, but that is very uneffective, because I have a lot of rows. (~100000)
1条答案
按热度按时间qyswt5oh1#
您可以尝试以下操作(使用
df
数据框):