I have data of cooking oil and its boiling temp and ranked it by highest boiling temp
print(df_ranked)
ranking oil boil_temp
0 1 avocado 270
1 2 sunflower 252
2 3 beef_tallow 250
3 3 butter_clarified 250
4 3 mustard 250
5 4 palm 235
6 5 corn 230
7 6 grapeseed 216
8 7 canola 204
9 8 coconut 200
10 9 olive_ev 160
11 10 butter 150
I used print function to print data with certain rank and this is the result
print(df_ranked.loc[df_ranked['ranking'].isin([3])])
ranking oil boil_temp
2 3 beef_tallow 250
3 3 butter_clarified 250
4 3 mustard 250
I want the result to be more simple like this
beef_tallow, 250
butter_clarified, 250
mustard, 250
or this
Oil with third highest boiling temperature: beef_tallow, butter_clarified, mustard
I tried using loop to get result like that, but the code I used seems to be wrong because it didn’t print anything
for i in df_ranked['ranking'].items():
for x in df_ranked['oil'].items():
if (i==3):
print(f'Oil with third highest boiling temperature:{x}')
>Solution :
If need all joined values first aggregate join and then loop in Series:
s = df.groupby('ranking')['oil'].agg(', '.join)
print (s)
1 avocado
2 sunflower
3 beef_tallow, butter_clarified, mustard
4 palm
5 corn
6 grapeseed
7 canola
8 coconut
9 olive_ev
10 butter
Name: oil, dtype: object
Oil with 1 highest boiling tem
for i, v in s.items():
print(f'Oil with {i} highest boiling temperature:{v}')
If need both columns join them and loop in groupby object:
g = df[['oil','boil_temp']].astype(str).agg(', '.join, axis=1).groupby(df['ranking'])
for i, v in g:
print(f"Oil with {i} highest boiling temperature:")
print('\n'.join(v))
Oil with 1 highest boiling temperature:
avocado, 270
Oil with 2 highest boiling temperature:
sunflower, 252
Oil with 3 highest boiling temperature:
beef_tallow, 250
butter_clarified, 250
mustard, 250
Oil with 4 highest boiling temperature:
palm, 235
Oil with 5 highest boiling temperature:
corn, 230
Oil with 6 highest boiling temperature:
grapeseed, 216
Oil with 7 highest boiling temperature:
canola, 204
Oil with 8 highest boiling temperature:
coconut, 200
Oil with 9 highest boiling temperature:
olive_ev, 160
Oil with 10 highest boiling temperature:
butter, 150