1

I need some help. I hope the below makes sense.

Imagine a dataframe with X number of rows and say 100 columns.

I want my function to arrange, in descending order, all the values in a given row. Then from those arranged values, give me the column-name of the first 10

Up to here, that is what the function below does for me.

def return_most_common_venues(row, 10):
    row_categories = row.iloc[1:]
    row_categories_sorted = row_categories.sort_values(ascending=False)

    return row_categories_sorted.index.values[0:10]

Additionally ...

The problem that I have is when the row has less than 10 positive values. Meaning the remaining are zeros.

From those first 10 arranged values, say position 9 and 10 are zeros. I would like that instead of giving me the column-name for those zero values, I would like it to to return a string saying 'Not Available'.

How can I modify the above function to return what I need.

Thank you for your help!

Efren M
  • 67
  • 5

1 Answers1

0

You could replace them in the list

row_cat = row_categories_sorted.index.values[0:10]
replaced_row_cat = ['Not Available' if not c else c for c in row_cat]
return replaced_row_cat

Or simply,

new_cat=[]
for cat in row_categories_sorted.index.values[0:10]:
    if cat == 0:
        new_cat.append("Not available")
    else:
        new_cat.append(c)
return new_cat
Adithya
  • 1,688
  • 1
  • 10
  • 18
  • Thanks for your prompt response Adithya. I should have mentioned that I'm new to coding. Do you mind clarifying if I should be replacing the "c" letters with other arguments? As is wont work. Thanks! – Efren M Apr 25 '20 at 00:45
  • Hi, I have re-post the question but better framed and illustrated. Follow this link: https://stackoverflow.com/questions/61420721/function-that-will-go-though-a-column-if-the-number-is-above-0-return-column-na – Efren M Apr 25 '20 at 03:49