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

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.

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 :

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