Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:17:18+00:00 2026-06-10T11:17:18+00:00

In R, there is a rather useful replace function. Essentially, it does conditional re-assignment

  • 0

In R, there is a rather useful replace function.
Essentially, it does conditional re-assignment in a given column of a data frame.
It can be used as so:
replace(df$column, df$column==1,'Type 1');

What is a good way to achieve the same in pandas?

Should I use a lambda with apply? (If so, how do I get a reference to the given column, as opposed to a whole row).

Should I use np.where on data_frame.values?
It seems like I am missing a very obvious thing here.

Any suggestions are appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:17:19+00:00Added an answer on June 10, 2026 at 11:17 am

    pandas has a replace method too:

    In [25]: df = DataFrame({1: [2,3,4], 2: [3,4,5]})
    
    In [26]: df
    Out[26]: 
       1  2
    0  2  3
    1  3  4
    2  4  5
    
    In [27]: df[2]
    Out[27]: 
    0    3
    1    4
    2    5
    Name: 2
    
    In [28]: df[2].replace(4, 17)
    Out[28]: 
    0     3
    1    17
    2     5
    Name: 2
    
    In [29]: df[2].replace(4, 17, inplace=True)
    Out[29]: 
    0     3
    1    17
    2     5
    Name: 2
    
    In [30]: df
    Out[30]: 
       1   2
    0  2   3
    1  3  17
    2  4   5
    

    or you could use numpy-style advanced indexing:

    In [47]: df[1]
    Out[47]: 
    0    2
    1    3
    2    4
    Name: 1
    
    In [48]: df[1] == 4
    Out[48]: 
    0    False
    1    False
    2     True
    Name: 1
    
    In [49]: df[1][df[1] == 4]
    Out[49]: 
    2    4
    Name: 1
    
    In [50]: df[1][df[1] == 4] = 19
    
    In [51]: df
    Out[51]: 
        1   2
    0   2   3
    1   3  17
    2  19   5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way of specifying a URI (rather than a physical path) as
Is there anyway to disable the rather annoying feature that Visual Studio (2008 in
Is there a way to INSERT multiple records rather than one at a time?
Is there some way when sending this message to specify that I rather have
There's a #pragma command to link in a library from the source file rather
Is there a performance benefit by passing ints by reference rather than by value
There is a web application for me to build, and rather than image map,
I'm inspecting a rather large server cluster. There are 15 odd servers running instances
Rather than doing my_var = my_var+'extra string'; is there a shorthand method like .=
I'd rather not make a call to the database to query @@IDENTITY. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.