Pandas – Expand Name & Value Columns

I’m not sure what the technical name of the desired operation is, but I have data in the following format where my field names are a column and the corresponding values are a column. Currently, I filtered a new df to each field name and then concat all three back together which is clunky and not ideal (I’m sure there is a better way).

dummy_data = {
'Field_Names': ['Field_1','Field_2','Field_3','Field_1','Field_2','Field_3'],
'Field_Values': ['Bob','John','Kyle','Eric','Pat','Rana']
}
dummy_df = pd.DataFrame(dummy_data)

I would like the field names to be their own columns. Desired output is below:

details = {'Field_1': ['Bob', 'Eric'],
           'Field_2': ['John', 'Pat'],
           'Field_3': ['Kyle', 'Rana']}

desired_output = pd.DataFrame(details)

print(desired_output)

There is likely a pandas method for this but again, I’m not sure what the technical name is for the operation I’m looking to perform. I am using Python 3.7+ in Jupyter on Windows.

>Solution :

Looks like you want to do a .groupby() on the field names and collect the field values in a list:

g = dummy_df.groupby('Field_Names')['Field_Values'].apply(list)
desired_output = pd.DataFrame({i:g.loc[i] for i in g.index.to_list()}

Update: My mistake that was only part of the solution. I’ve updated the code above. My output now:

    Field_1 Field_2 Field_3
0   Bob    John Kyle
1   Eric    Pat Rana

Leave a Reply