Skip to content
Advertisement

Function that takes n rows as input and returns column names if sum in column equals n

I have a large DataFrame that structures as follows:

JavaScript

I am trying to build a function that takes in n row names as arguments sums up the values in all columns and returns me the column names if the sum of those columns equals n.

For instance, using label1, label2 and label3 as inputs I would like to obtain the following output:

JavaScript

or

JavaScript

I have little knowledge of building functions in Python, but got my head really stuck on this. Could you kindly help me to progress?

Advertisement

Answer

Filter rows by loc and test if all 1 per columns, then filter index of Series:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement