Python Forum
Pandas Dataframe Filtering based on rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas Dataframe Filtering based on rows
#1
New to python and this forum Smile

I have a dataset like this:

df1 = pd.DataFrame({'opA': [1,1,1,1,0], 
           'opB': [1,1,1,0,1],
            'opC': [1,1,1,1,2], 
           'opD': [0,1,1,0,3],
           'Active': ['opC','opD', 'opD', 'opC', 0]})
df1 = df1.rename(index={df1.last_valid_index() : 'Priority'})
df1.loc['Priority','Active'] = ''
print(df1)
The Active column consists of the OPcolumn name that has the max value in each row, while taking into factor the 'Priority' of each OPcolumn. For this, I have this code working:

df = df.sort_index(axis=1,key=lambda x:df.loc['Priority',x],ascending=False)
df['Active'] = df.idxmax(axis=1)
Now I need to do this:

df1 = pd.DataFrame({'opA': [1,1,1,1,0,0], 
           'opB': [1,1,1,0,1,0],
            'opC': [1,1,1,1,2,0], 
           'opD': [0,1,1,0,3,3],
           'Active': ['opC','opC', 'opC', 'opC', 0,0]})
df1 = df1.rename(index={df1.last_valid_index() - 1 : 'Priority'})
df1 = df1.rename(index={df1.last_valid_index() : 'minOccurrence'})
df1.loc['Priority','Active'] = ''
df1.loc['minOccurrence','Active'] = ''
print(df1)
Since opD doesn't have 3 straight "Actives" it isn't active at index 1 or 2 where previously it was Active based on 'Priority' column only.

vs. if opD had a 1 at index 0.

df1 = pd.DataFrame({'opA': [1,1,1,1,0,0], 
           'opB': [1,1,1,0,1,0],
            'opC': [1,1,1,1,2,0], 
           'opD': [1,1,1,0,3,3],
           'Active': ['opD','opD', 'opD', 'opC', 0,0]})
df1 = df1.rename(index={df1.last_valid_index() - 1 : 'Priority'})
df1 = df1.rename(index={df1.last_valid_index() : 'minOccurrence'})
df1.loc['Priority','Active'] = ''
df1.loc['minOccurrence','Active'] = ''
print(df1)
How do I do this? The minOccurrence row can have any values not just 0,0,0,3. (e.g. 0,1,3,2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add NER output to pandas dataframe dg3000 0 93 Apr-22-2024, 08:14 PM
Last Post: dg3000
  Merging rows and adding columns based on matching index pythonnewbie78 3 825 Dec-24-2023, 11:51 AM
Last Post: Pedroski55
  HTML Decoder pandas dataframe column mbrown009 3 1,055 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,127 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas dataframe: calculate metrics by year mcva 1 2,327 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,271 Jan-30-2022, 10:21 AM
Last Post: anto5
  Python Based ETL Using Pandas HyperSpeed 0 1,579 Jan-13-2022, 04:18 PM
Last Post: HyperSpeed
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,808 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,558 Jan-10-2022, 04:42 PM
Last Post: moduki1
  New Dataframe Column Based on Several Conditions nb1214 1 1,814 Nov-16-2021, 10:52 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