0

I need to convert data in a pandas dataframe in a single column into a dataframe with multiple columns.

Original Dataframe Data:

Date Type Value
10/14/2022 12:35:00 PM TypeA 1
10/14/2022 12:35:00 PM TypeB 2
10/14/2022 12:35:00 PM TypeC 4
10/15/2022 12:35:00 PM TypeA 6
10/15/2022 12:35:00 PM TypeB 17
10/15/2022 12:35:00 PM TypeC 4
10/16/2022 12:35:00 PM TypeA 3
10/16/2022 12:35:00 PM TypeB 1
10/16/2022 12:35:00 PM TypeC 12

Here is what the data needs to look like when rearranged:

Date TypeA TypeB TypeC
10/14/2022 12:35:00 PM 1 2 4
10/15/2022 12:35:00 PM 6 17 4
10/16/2022 12:35:00 PM 3 1 12

Here is the code to create the original dataframe:

import pandas as pd
        
        
    
data = [['10/14/2022 12:35:00 PM', 'TypeA', 1],
     ['10/14/2022 12:35:00 PM', 'TypeB', 2],
    ['10/14/2022 12:35:00 PM', 'TypeC', 4],
    ['10/15/2022 12:35:00 PM', 'TypeA', 6],
    ['10/15/2022 12:35:00 PM', 'TypeB', 17],
     ['10/15/2022 12:35:00 PM', 'TypeC', 4],
    ['10/16/2022 12:35:00 PM', 'TypeA', 4],
    ['10/16/2022 12:35:00 PM', 'TypeB', 1],
    ['10/16/2022 12:35:00 PM', 'TypeC', 12]]
        
        
    
df = pd.DataFrame(data, columns=['DateTime', 'Type', 'Value'])

How do I programmatically convert df to the desired arrangement?

  • 1
    It matters quite a bit whether your original `df` looks like the *table* you've supplied, or like the *code* you've supplied. For the first `df`, you can do: `df.pivot(index='Date', columns='Type', values='Value')`. For your second (code) `df` this won't work, because it has duplicates. – ouroboros1 Oct 16 '22 at 15:57
  • Thanks for the comment. The table was correct and I edited the python code to match. – Matthew David Jankowski Oct 16 '22 at 16:00
  • 1
    Does this answer your question? [How can I pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe) – ouroboros1 Oct 16 '22 at 16:03
  • Yep this is perfect. Thank you! I didn't know the term for what I was trying to do! – Matthew David Jankowski Oct 16 '22 at 16:04
  • 1
    Thanks. So, that would be a basic use of `df.pivot`, which you can find in the suggested duplicate: `res = df.pivot(index='DateTime', columns='Type', values='Value').reset_index()`. Add `res.columns.name = None` to get rid of `columns.name`. – ouroboros1 Oct 16 '22 at 16:04

0 Answers0