将通过列的函数,如果数字大于0,则返回列名称,但当数字为0时,返回“不可用” [英] Function that will go though a column, if the number is above 0 return column name, but when is 0 return "Not Available'
问题描述
我需要一些帮助。
假设我有以下名为 venues_df 的数据框,
Let's say I have the below dataframe called venues_df
我也有此功能: return_most_common_venues
def return_most_common_venues(row, 4):
# Selects the row values
row_values = row.iloc[1:]
# Sorts the selected row values
row_values_sorted = row_values.sort_values(ascending=False)
# Returns the column name of the first 4 sorted values
return row_values_sorted.index.values[0:4]
如果我将函数应用于第一行:
If I apply my function on the first row:
return_most_common_venues(venues_df.iloc[0, :], 4)
结果将是一个数组(下表用于illustrati
The result will be an array (the below tables are for illustration purposes):
array(['Bar','Restaurant','Park','Gym'])
array (['Bar', 'Restaurant', 'Park', 'Gym'])
问题是当我将函数应用于第二行时。
The problem is when I apply my function to the second row.
return_most_common_venues(venues_df.iloc[1, :], 4)
我会得到
array(['Park','Restaurant','Gym','SuperMarket'])
array(['Park', 'Restaurant', 'Gym', 'SuperMarket'])
我需要它返回:
数组(['Bar','Restaurant','Not Available','Not Available'])
如果该值为零,我需要它返回不可用,而不是列名 Gym和 SuperMarket
If the value is zero I need it to return 'Not Available' instead of the column names "Gym' and 'SuperMarket'
如何我可以修改我的函数以返回我需要的东西吗?
How can I modify my function to return what i need?
感谢您的帮助!
Efren
推荐答案
def return_most_common_venues(df, row, cols):
# Selects the row values
row_values = df.loc[row]
# Sorts the selected row values
row_values_sorted = row_values[np.argsort(row_values)[-cols:]][::-1]
# Returns the column name of the first 4 sorted values
return [index if value > 0 and value != np.nan else "Not Available" for index, value in zip(row_values_sorted.index, row_values_sorted.values)]
return_most_common_venues(df, row=1, cols=4)
输出:
['Park', 'Restaurant', 'Not Available', 'Not Available']
这篇关于将通过列的函数,如果数字大于0,则返回列名称,但当数字为0时,返回“不可用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!