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

Python Pandas Dataframe Row name Change

How can I replace the first index 0 with "Color" and 1 with "Size"?

import pandas as pd
clothes = {"shirt": ["red", "M"], "sweater": ["yellow", "L"], "jacket": ["black", "L"]}

# pd.DataFrame.from_dict(clothes)
df = pd.DataFrame.from_dict(clothes)
df

I tried like this but nothing changes. The output shows index name as an error

df = pd.DataFrame.from_dict(clothes, index = ['color', 'size'])

>Solution :

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

don’t use from_dict, but the DataFrame constructor directly:

df = pd.DataFrame(clothes, index=['color', 'size'])

If the DataFrame is already existing, set_axis:

df = pd.DataFrame(clothes)

df = df.set_axis(['color', 'size'])

output:

      shirt sweater jacket
color   red  yellow  black
size      M       L      L
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