Select a specific cell in a csv file and return the column and row details in pandas in python -


i have pandas dataframe details mentioned below.

names, oil, fat, salt salad,  0.2, 0.1,  0.8 bread,  0.1, 0.9,  0.1 

for each row want find minimum number of value value , return row , column name separately. output should follows.

e.g., ['salad', 'fat'] [['bread', 'oil'], ['bread', 'salt']] 

that is, if consider row 1 lowest number 0.1. row , column headings salad , fat. row 2 lowest number 0.1. row , column headings bread , oil , bread , salt.

i interested in knowing if there easy way of doing in pandas.

you can using lambda function , apply each row:

df.apply(lambda row: [[row.name, l] l in row[row == row.min()].index], axis=1).tolist() 

if names not index, use:

df.set_index('names').apply(lambda row: [[row.name, l] l in row[row == row.min()].index], axis=1).tolist() 

assuming index 'names'.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -