Python Forum
Updating df rows based on 2 conditions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating df rows based on 2 conditions
#1
First post, so I apologize for any improper post etiquette, and am receptive to feedback. I have a data frame containing NBA games from the past several seasons(6000+ rows) and need to update some values to adjust for teams who have built new arenas. Data set looks like this

Output:
Date Visitor V_PTS Home H_PTS \ 0 2012-10-30 19:00:00 Washington Wizards 84 Cleveland Cavaliers 94 1 2012-11-02 19:30:00 Chicago Bulls 115 Cleveland Cavaliers 86 2 2012-11-17 19:30:00 Dallas Mavericks 103 Cleveland Cavaliers 95 Attendance Arena Location Capacity Yr Arena Opened \ 0 20562 Quicken Loans Arena Cleveland, Ohio 20562 1994 1 20562 Quicken Loans Arena Cleveland, Ohio 20562 1994 2 18633 Quicken Loans Arena Cleveland, Ohio 20562 1994 Season 0 2012-13 1 2012-13 2 2012-13
Here is what I have so far for a function I am trying to apply with no success. Any help would be appreciated on what I am doing wrong or if there is a better way to do this and also how to apply the function properly to the df which I named NBA.

def old_arena(home):
    for team in home :
        if (team == ('Detroit Pistons') & (nba['Date']<'2016-9-30')):
            nba['Arena'] = 'The Palace of Auburn Hills'
            nba['Location'] = 'Auburn Hills, Michigan'
            nba['Capacity'] = 22076
            nba['Yr Arena Opened'] = 1988
        elif ((team == 'Sacremento Kings') & (nba['Date']<'2017-9-5')):
            nba['Arena'] = 'Sleep Train Arena'
            nba['Capacity'] = 17317
            nba['Yr Arena Opened'] = 1988
Reply
#2
You are abusing pandas - here's an example of selecting rows by condition - assuming that your column Date is of
dtype=datetime64
games[(games['Date'] < pd.Timestamp('2016-9-30')) & (games['Visitor'] == 'Chicago Bulls')]
You cannot change cells in a row like that - but that (at the moment) is beyond my knowledge

PS Googled
Quote:update selected dataframe row
, got this answer, elementary Watson
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merging rows and adding columns based on matching index pythonnewbie78 3 811 Dec-24-2023, 11:51 AM
Last Post: Pedroski55
  Pandas Dataframe Filtering based on rows mvdlm 0 1,430 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  New Dataframe Column Based on Several Conditions nb1214 1 1,800 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  updating cluster of elements based on the max value of distance alex80 0 1,592 Oct-02-2020, 11:11 AM
Last Post: alex80
  Extracting rows based on condition on one column Robotguy 2 2,203 Aug-07-2020, 02:27 AM
Last Post: Robotguy
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 2,215 Aug-03-2020, 02:05 AM
Last Post: scidam
  Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 2,153 Jun-09-2020, 06:05 AM
Last Post: Pan
  Grouping data based on rolling conditions kapilan15 0 1,951 Jun-05-2019, 01:07 PM
Last Post: kapilan15
  Pandas fillna based on conditions amyd 1 15,888 May-03-2019, 11:27 AM
Last Post: scidam
  Removing rows at random based on the value of a specific column Mr_Keystrokes 4 5,599 Aug-24-2018, 11:15 AM
Last Post: Mr_Keystrokes

Forum Jump:

User Panel Messages

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