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

How do I "push down" the current columns to form the first row, and create new columns to replace that one?

I have a DataFrame that essentially has the first row that I want as the column row and I’d like to know how to set new columns and set that row as the first row.

For example:

|  4  |  3  | dog |
| --- | --- | --- |
|  1  |  2  | cat |

I want to change that DataFrame to be:

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

| number_1 | number_2 | animal |
| -------- | -------- | ------ |
|     4    |     3    |   dog  |
|     1    |     2    |   cat  |

What would be the best way to do this?

>Solution :

Lets create a new dataframe with old column row as the first row followed by remaining rows

pd.DataFrame([df.columns, *df.values], columns=['num_1', 'num_2', 'animal'])

   num_1  num_2 animal
0      4      3    dog
1      1      2    cat
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