Python Forum
compare and modify columns in dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compare and modify columns in dataframe
#1
Hello all,

I'm have loaded a csv file into a dataframe using pandas. I want to compare the numbers in two columns, if col A is the same as col b, do this. If Col a is > col b, do this. and so on.

I'm pretty sure there is a shorter way to do this. Here is my code:

Thanks!!

for index,row in puntos.iterrows():

    if row["localGoals"] > row["visitorGoals"]:        
        puntos["PuntosLocal"] = 3
        puntos["PuntosVisitante"] = 0
        print(row["localGoals"])
    elif row["localGoals"] < row["visitorGoals"]:
        puntos['PuntosLocal'] = 0
        puntos['PuntosVisitante'] = 3
    else:
        puntos['PuntosLocal'] = 1
        puntos['PuntosVisitante'] = 1
Reply
#2
Hi DionisiO,

There are a number of ways to write shorter versions of what your code does (at least without the [printing), for example...

In four lines:
puntos.loc[puntos.localGoals > puntos.visitorGoals, 'PuntosLocal'] = 3
puntos.loc[puntos.localGoals < puntos.visitorGoals, 'PuntosLocal'] = 0
puntos.loc[puntos.localGoals == puntos.visitorGoals, 'PuntosLocal'] = 1
puntos['PuntosVisitante'] = puntos.PuntosLocal.map(lambda pl: {3: 0, 0: 3, 1: 1}[pl])
or in "one" line:
puntos['PuntosLocal'], puntos['PuntosVisitante'] = zip(*[
    (3, 0) if local > visitor else (0, 3) if local < visitor else (1, 1)
    for local, visitor in zip(puntos.localGoals, puntos.visitorGoals)
])
On the other hand, I read your code and knew exactly what it did within a couple seconds. Can you honestly do that for either of my options? Your code is reasonably efficient, and very readable - not too bad!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add columns to polars dataframe sayyedkamran 1 1,687 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 776 Oct-03-2023, 09:29 PM
Last Post: flash77
  Convert several columns to int in dataframe Krayna 2 2,362 May-21-2021, 08:55 AM
Last Post: Krayna
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,572 Jan-27-2021, 10:59 AM
Last Post: epsilon
  Adapting a dataframe to the some of columns flyway 2 2,032 Aug-12-2020, 07:21 AM
Last Post: flyway
  Difference of two columns in Pandas dataframe zinho 2 3,313 Jun-17-2020, 03:36 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 2,074 Mar-18-2020, 06:07 AM
Last Post: mani
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,414 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 1,915 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  newbie: loop, modify dataframe cells expat_th 5 3,620 Mar-03-2020, 09:05 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020