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?
>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/