in Education by
Currently I'm returning column name of the max value in the each row. df['Active'] = df.idxmax(axis=1) How do I take into account the Priority for each column? e.g. for Row 0, the Active column should have opC since it has a higher priority than opA. (Also Priority row shouldn't return anything in the Active column). Update: Follow up scenario. Adding an additional row called 'minOccurrence'. Here's an example of it. 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. 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) 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) 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) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You need to resort the columns before using idxmax temp_cols = df.columns df = df.sort_index(axis=1,key=lambda x:df.loc['Priority',x],ascending=False) df['Active'] = df.idxmax(axis=1) df = df[list(temp_cols)+['Active']] df.loc['Priority','Active'] = ''

Related questions

0 votes
    Currently I'm returning column name of the max value in the each row. df['Active'] = df.idxmax( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Say I have a dictionary that the key of year, and the corresponding value of a list of values. Is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have a simple method to search a pandas dataframe column for a list of keywords; however, I'd like to create a ... do everyth 28,passei o dia com o meu amor comemo demai...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I have a dataset with employee payroll information (df2). It has a date, job title, shift start ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a pandas series containing a list of dictionaries. I'd like to parse the contents of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    I am trying to create a pandas dataframe that combines all children into one row class Parent(Base): ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have a csv file and I am reading this file in python using pandas. I want to read each row of ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am utilizing the following to find missing values in my spark df: from pyspark.sql.functions import col, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Problem: Select first N rows and last row in Python Pandas. only gets 1 row which is index 9 and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I'm trying to get the number of rows of dataframe df with Pandas, and here is my code. Method 1: total_rows ... What am I doing wrong? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    18F-AV-1451-A07 Value refer to another sheet called "CONTENT" in which column "B" and row "3". ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have csv file and that looks like following. I want to remove all rows before one row values [ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
...