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

  • SEARCH
  • Home
  • 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 8358707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:53:15+00:00 2026-06-09T10:53:15+00:00

I have a Pandas DataFrame with four columns, A, B, C, D . It

  • 0

I have a Pandas DataFrame with four columns, A, B, C, D. It turns out that, sometimes, the values of B and C can be 0. I therefore wish to obtain the following:

B[i] = B[i] if B[i] else min(A[i], D[i])
C[i] = C[i] if C[i] else max(A[i], D[i])

where I have used i to indicate a run over all rows of the frame. With Pandas it is easy to find the rows which contain zero columns:

df[df.B == 0] and df[df.C == 0]

however I have no idea how to easily perform the above transformation. I can think of various inefficient and inelegant methods (for loops over the entire frame) but nothing simple.

  • 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-09T10:53:18+00:00Added an answer on June 9, 2026 at 10:53 am

    A combination of boolean indexing and apply can do the trick.
    Below an example on replacing zero element for column C.

    In [22]: df
    Out[22]:
       A  B  C  D
    0  8  3  5  8
    1  9  4  0  4
    2  5  4  3  8
    3  4  8  5  1
    
    In [23]: bi = df.C==0
    
    In [24]: df.ix[bi, 'C'] = df[bi][['A', 'D']].apply(max, axis=1)
    
    In [25]: df
    Out[25]:
       A  B  C  D
    0  8  3  5  8
    1  9  4  9  4
    2  5  4  3  8
    3  4  8  5  1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pandas dataframe object that looks like this: one two three four
I have a pandas DataFrame that has multiple columns in it: Index: 239897 entries,
I have a pandas DataFrame and I want to plot a bar chart that
I have the following Pandas Dataframe with a MultiIndex(Z,A): H1 H2 Z A 0
I have a pandas DataFrame with a date column. It is not an index.
I have the data below saved as a pandas dataframe . With this data,
I have a DataFrame with a few columns. One columns contains a symbol for
Have an app that can use tts to read text messages. It can also
I have a times series with temperature and radiation in a pandas dataframe .
I need to store Python decimal type values in a pandas TimeSeries / DataFrame

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.