3

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], } ) 
1

1 Answer 1

2

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 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I came back to my own data, and this is what I have tried (I should have tested it on this may be not reproducible example before asking). I will see what is different in my own data and why I can not reproduce this on them
Yes, a reproducible example for which this doesn't work would be useful
It was just a stupid mistake... i did not save the dataframe with the renamed variable. Thank you again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.