0

I want to rename all of my rows names at once in python

This is example ( in R ) of what I want :

names(dataframe) <- c(paste0("G",1:7129),"class")
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • What are you importing and creating? The `R` analogy is next to useless in specifying what you want. – hpaulj Nov 03 '21 at 07:43

3 Answers3

0

You can use pandas rename function to rename columns like:

df.rename(columns={'old_column1_name':'new_col1_name','old_column2_name':'new_col2_name'}, inplace=True)

If you have thousands of columns and want the names like col1, col2, etc do something like:

cols = list(df.columns)
df.rename(columns={cols[i]:'new col'+str(i) for i in range(len(cols)) } , inplace=True)
0

In python you can change columns using pandas like this :

df.rename(columns={"A": "a", "B": "c"})

If you want rename index :

df.rename(index={0: "x", 1: "y", 2: "z"})

0

For renaming index and indexes, I found the two line of code i.e.,

   df=df.rename(index=lambda x:"G"+str(x+1))
   df=df.rename_axis("class") 

Below is the complete example,

import pandas as pd

df=pd.DataFrame({'Full Name':['Tom', 'nick', 'krish', 'jack'],
        'salary per year':[20, 21, 19, 18],
    'F1 Name':['Tom', 'nick', 'krish', 'jack'],
    'F2 Name':['Tom', 'nick', 'krish', 'jack'],
    'F3 Name':['Tom', 'nick', 'krish', 'jack'],
    'F4 Name':['Tom', 'nick', 'krish', 'jack']
})

print(df)
df=df.rename(index=lambda x:"G"+str(x+1))
df=df.rename_axis("class")
print(df)

Output of the above program is :

    Full Name  salary per year F1 Name F2 Name F3 Name F4 Name
0       Tom               20     Tom     Tom     Tom     Tom
1      nick               21    nick    nick    nick    nick
2     krish               19   krish   krish   krish   krish
3      jack               18    jack    jack    jack    jack
Full Name  salary per year F1 Name F2 Name F3 Name F4 Name
class                                                           
G1          Tom               20     Tom     Tom     Tom     Tom
G2         nick               21    nick    nick    nick    nick
G3        krish               19   krish   krish   krish   krish
G4         jack               18    jack    jack    jack    jack

Here, this thread, you can find different ways to replace white space to '_'.

For rename index : method of data frame: rename_axis

For renaming columns or index patterns of data frames is : rename

Hopefully, now it resolves your problem, :)

Shubhankar
  • 72
  • 1
  • 10
  • Dear Fellow! thanks for your help although it was not very helping hehe. I have used lambda expression to rename my whole data set. I wanted to add "gene" to the index name, also my serial number started from '0' and I wanted to make it '1' I have started learning python 1 week ago so I'm very much bad at explaining what I want – Hifsa Mehtab Nov 04 '21 at 13:17
  • and about R code this code is adding names to the index/row of my dataset it will work like G1 , G2 , G3 , G4 , G5 upto G7129 and the last row (7139) will be named "class " this all will happen in single line code. – Hifsa Mehtab Nov 04 '21 at 13:23
  • furthermore, after a lot more searching I came to a point where I named my index as 1,2,3,4, and so on by using this line df.rename(index = lambda x: x + 1) but still I want 'gene' added – Hifsa Mehtab Nov 04 '21 at 13:32
  • For more related index queries, you can visit this [Rename Pandas DataFrame Index](https://stackoverflow.com/questions/19851005/rename-pandas-dataframe-index) – Shubhankar Nov 08 '21 at 07:40