Search code examples
pythonrpandasrename

rename a variable and "select" this variable using a string store in a vector


I am a R user trying to learn python.

For some analysis, I used to rename dataframe variable like this

library(dplyr) 

variable = "c"
df = data.frame(a=c(8,5,7,8), b=c(9,6,6,8), c=c(0,7,8,9))

> df
  a b c
1 8 9 0
2 5 6 7
3 7 6 8
4 8 8 9

out = df %>% rename(variable := !!variable)

> out
 
 a b variable
1 8 9        0
2 5 6        7
3 7 6        8
4 8 8        9

I don't find way to do the same in python. Any idea to help me ?

import pandas as pd

df = pd.DataFrame(
    {
        "a": [8,5,7,8],
        "b": [9,6,6,8],
        "c": [0,7,8,9],
       
    }
)

Solution

  • You can do the same with rename and a dictionary, the order of the "parameters" is just reversed compared to R:

    variable = 'c'
    out = df.rename(columns={variable: 'variable'})
    

    Or, without the intermediate:

    out = df.rename(columns={'c': 'variable'}) # df %>% rename(variable := c)
    

    Output:

       a  b  variable
    0  8  9         0
    1  5  6         7
    2  7  6         8
    3  8  8         9