Python equivalent to replace function in R

0 votes
I know that there is replace() function in R, which helps me to replace specific values based on a condition. Is there an equivalent to that in python?
May 30, 2018 in Data Analytics by nirvana
• 3,060 points
70 views

1 answer to this question.

0 votes

There is actually a replace function in python too.

Let me demonstrate it with an example:

    A  B
0  1  1
1  2  2
2  3  3

The value '3' in column 'B' can be replaced like this:

my_df['B']=my_df['B'].replace(3, 30)

   A   B
0  1   1
1  2   2
2  3  30
answered May 30, 2018 by Bharani
• 4,550 points

Related Questions In Data Analytics

0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
138 views
0 votes
1 answer

Which function can I use to clear the console in R and RStudio ?

Description                   Windows & Linux           Mac Clear console                      Ctrl+L ...READ MORE

answered Apr 17, 2018 in Data Analytics by anonymous
1,332 views
0 votes
1 answer

Which package is used to do data import in R and Python and How do you import SAS data?

We can do data import using multiple ...READ MORE

answered Aug 24, 2018 in Data Analytics by Anmol
• 3,620 points
32 views
0 votes
1 answer

How to replace all occurrences of a character in a character column in a data frame in R

If you used sub() to replace the ...READ MORE

answered Jun 28 in Data Analytics by anonymous
• 25,820 points
48 views
0 votes
1 answer

Changing Pandas data-frame to JSON type

abc.to_json(orient='records') This command will give you the desired ...READ MORE

answered May 22, 2018 in Data Analytics by anonymous
78 views
0 votes
2 answers
0 votes
1 answer

Converting a pandas data-frame to a dictionary

Emp_dict=Employee.to_dict('records') You can directly use the 'to_dict()' function ...READ MORE

answered May 23, 2018 in Data Analytics by Bharani
• 4,550 points
1,296 views
0 votes
4 answers

Python vs. R for data science

I would say both Python and R ...READ MORE

answered Aug 1 in Data Analytics by briny
105 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE) ...READ MORE

answered Aug 14 in Data Analytics by anonymous
74 views
0 votes
1 answer

Applying the same function to every row of a data.frame - R

You can use the 'appply()' function for ...READ MORE

answered May 22, 2018 in Data Analytics by Bharani
• 4,550 points
53 views