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

Question:

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:

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

What would be the best way to do this?

Asked By: Sean

||

Answers:

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
Answered By: Shubham Sharma
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.