0

super noob here. Kinda left high & dry on some code that I am trying to fix with my very limited knowledge. Currently stuck on some python pandas code...

closest_price_final = pd.DataFrame(closest_price_df)
closest_price_final.columns = ['Label', 'Price Distance']
print(closest_price_final)

   Label  Price Distance
0    A1       47.033366
1    A2      104.566732
2    B1      176.803385
3    B2      234.336751
4    A3      306.573405
5    A4      306.573405
6    B3      -25.203288
7    A5      -82.736654
8    B4     -154.973307
9    A6     -212.506673
10   A7     -212.506673

formatted list image above here

From the list above, I need to get the (min) negative label for one use case (a) and the (min) positive label for use case (b).

if use case == 'a': script something to return "B3" if use case == 'b': script something to return "A1"

Apologies in advance for my primitive linguistics, I'm trying my best. Was told to try idxmin() - ha way over my head. Any guidance would be HUGE for me. Thank you!

Marat
  • 15,215
  • 2
  • 39
  • 48

1 Answers1

0

Hope this helps

#negative part of prices
closest_price_final_neg = closest_price_final[closest_price_final['Price']<0]

#positive part of prices
closest_price_final_pos = closest_price_final[closest_price_final['Price']>0]

#getting minimums of each 
minimum_neg = closest_price_final_neg.loc[closest_price_final_neg['Value'].idxmin()]['Label']    
minimum_pos= closest_price_final_pos.loc[closest_price_final_pos['Value'].idxmin()]['Label'] 
João Ramiro
  • 312
  • 1
  • 9
  • I assume it is typo but you wrote `closest_price_final_neg` in the section `getting minimums of each` for both values, also it seems that the OP wants the the label associated to the min value more than the value itself – Ben.T May 05 '20 at 23:54
  • Ben is absolutely right - we're getting close, but I do need the actual label. I read a bit more and found this to be pulling the correct price distance, but not the correct corresponding label: above_zero = closest_price_final[closest_price_final['Price Distance'] > 0].min() below_zero = closest_price_final[closest_price_final['Price Distance'] < 0].min() – YongheChang May 06 '20 at 00:01
  • In that case you will want to use idxmin. Like in here https://stackoverflow.com/questions/29919306/find-the-column-name-which-has-the-maximum-value-for-each-row – João Ramiro May 06 '20 at 01:03