I have a project including a big dataset and I am trying to extract some values, and this values depend on the input which is given by the user, that means it differs from one to another. My dataframe has 16 columns including species, weight, population, color, locations etc. and looks like (I made a small example for this question)
df = pd.DataFrame({'species': ['bear', 'bear', 'marsupial', 'polar bear', 'bear', 'polar bear'],
'weights': [350, 350, 140, 450, 350, 540],
'population': [1864, 22000, 80000, 3000, 7864, 5000],
'color': ['brown', 'black', 'gray', 'white', 'brown', 'brown'],
'locations': ['US', 'Canada', 'Canada', 'Russia', 'US', 'Canada']})
output:
color locations population species weights
0 brown US 1864 bear 350
1 black Canada 22000 bear 350
4 brown US 7864 bear 350
I ask from the user which features do you want to see, and I return the name/s from the dataframe. I collect the user input as a dictionary:
dict = {
species: bear,
weights: 350
}
and I can get the names manually by using loc
df_loc = df.loc[(df['weights'] == 350) & (df['species'] == 'bear')]
output:
color locations population species weights
0 brown US 1864 bear 350
1 black Canada 22000 bear 350
4 brown US 7864 bear 350
Til now, it is fine, but I couldn't figure out a way to put df.loc[] in a loop through the dictionary and which can automatize the search process, since the size and the keys-values in this dictionary can change all the time.
Any ideas?