Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert rows of data to headers in python while keeping the data

I am trying to write a python script to transform some csv data.
Didn’t get too far using groupby or multiindex.
I want to go from this:

A, 5000
B, 6000
C, 8000
D, 6000
A, 3000
B, 2000
C, 6000
D, 1000
A, 4000
B, 1000
C, 1000
D, 3000

To this:

A      B      C      D
5000   6000   8000   6000
3000   2000   6000   1000
4000   1000   1000   3000

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can convert the data to a dataframe using df.pivot in the format you requested and output the data to a CSV.

import pandas as pd

data = [
    ['A', 5000],
    ['B', 6000],
    ['C', 8000],
    ['D', 6000],
    ['A', 3000],
    ['B', 2000],
    ['C', 6000],
    ['D', 1000],
    ['A', 4000],
    ['B', 1000],
    ['C', 1000],
    ['D', 3000]
]

# Create a dataframe from the data
df = pd.DataFrame(data, columns=['Letters', 'Data'])

# Pivot the dataframe to make letters as column headers
df_pivot = df.pivot(index=None, columns='Letters', values='Data')

# Save the pivoted dataframe to a CSV file
df_pivot.to_csv('output.csv', index=False)

Output:

A, B, C, D
5000, 6000, 8000, 6000
3000, 2000, 6000, 1000
4000, 1000, 1000, 3000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading