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 csv File format with pandas?

I am not familar with pandas, now.
I have a csv file, which has the format:

USER; ROLE
hugo; role_x
hugo; role_y
otto; role_x

and I need the output in this form:

USER; ROLES
hugo; role_x, role_y
otto; role_x

I do this with iteration and dictionary. Is this easier by pandas?

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 :

Pandas is great for data aggregation and manipulation, you can group by user very easily:

import pandas as pd

# Read the CSV file into a pandas DataFrame
df = pd.read_csv('your_file.csv', sep=';')

# Do your grouping
result_df = df.groupby('USER')['ROLE'].agg(', '.join).reset_index()

# Change the 'ROLE' column to 'ROLES'
result_df.rename(columns={'ROLE': 'ROLES'}, inplace=True)

# Save the result
result_df.to_csv('output_file.csv', sep=';', index=False)

Here are some nice examples for how to use the groupby function:
https://www.geeksforgeeks.org/python-pandas-dataframe-groupby/

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