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!

3
  • You need to split the labels to extract the first letter, right? Can you be more specific about which part you're struggling with? Commented May 5, 2020 at 23:28
  • Can you explain a bit more why you are getting B3 and A1? Commented May 5, 2020 at 23:38
  • It does @JoãoRamiro - Thank you! Commented May 6, 2020 at 1:50

1 Answer 1

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'] 
Sign up to request clarification or add additional context in comments.

3 Comments

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 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()
In that case you will want to use idxmin. Like in here stackoverflow.com/questions/29919306/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.