Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

278
Views
other than max value, replace all value to 0 in each column (pandas, python)

i have a df which I want to replace values that are not the max value for each column to 0.

code:

data = {
    "A": [1, 2, 3],
    "B": [3, 5, 1],
    "C": [9, 0, 1]
}

df = pd.DataFrame(data)

sample df:

   A  B   C
0  1  3   9
1  2  5   0
2  3  1   1

result trying to get:

   A  B   C
0  0  0   9
1  0  5   0
2  3  0   0

kindly advise. many thanks

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use DataFrame.where with compare max values :

df = df.where(df.eq(df.max()), 0)
print(df)
   A  B  C
0  0  0  9
1  0  5  0
2  3  0  0
over 4 years ago · Santiago Trujillo Report

0

Try:

df[df!=df.max()] = 0

Output:

   A  B  C
0  0  0  9
1  0  5  0
2  3  0  0
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!