Skip to content
Advertisement

How to rename the rows in dataframe using pandas read (Python)?

I want to rename rows in python program (version – spyder 3python 3.6) . At this point I have something like that:

JavaScript

Before that i wanted to rename my columns:

JavaScript

It gave me something like that.

JavaScript

But now, I want to rename rows. I want:

JavaScript

How can I do it? The main idea is to rename every row created by pd.read by the data in the B column. I tried something like this:

JavaScript

but it’s not working.

Any ideas? Maybe just replace the data frame rows by column B? How?

Advertisement

Answer

I think need set_index with rename_axis:

JavaScript

Solution with rename and dictionary:

JavaScript

If default RangeIndex solution should be:

JavaScript

Output:

JavaScript

EDIT:

If dont want column B solution is with read_csv by parameter index_col:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement