Struggling to remove rows from my dataframe

巧了我就是萌 提交于 2021-01-29 11:05:02

问题


I've been Googling and reading this site, and I know folks say that, in general, if you're iterating over a Dataframe, you're likely doing it wrong. So, I figure I'm doing it wrong. The challenge I'm presented with is that I need to make decisions on what to keep versus what to discard based on logic. Best explained with an example. Say my Dataframe looks like:

SO_NUMBER   ITEM #   SALES_QTY   PO_NUMBER   PO_QTY   PO_PRICE   ENOUGH_TO_FILL
--------------------------------------------------------------------------
123         ABC123   5           555         1000     8.88       TRUE
312         ABC123   10          555         1000     8.88       TRUE
123         ABC123   5           777         500      1.11       TRUE
312         ABC123   10          777         500      1.11       TRUE  

My Dataframe was constructed by merging my Sales Date with my Purchasing Data. The challenge I'm faced with is making sure that I record the MAXIMUM PO_PRICE for every Sales Order, without exceeding the total PO Quantity. So, for the above data, my result would be, since I had enough to fill:

SO #   ITEM #   SALES_QTY   PO_NUMBER   PO_QTY   PO_PRICE
----------------------------------------------------------
123    ABC123   5           555         1000     8.88     
312    ABC123   10          555         1000     8.88     

But, I'm screwed in the following scenario:

SO_NUMBER   ITEM #   SALES_QTY   PO_NUMBER   PO_QTY   PO_PRICE   ENOUGH_TO_FILL
--------------------------------------------------------------------------
123         ABC123   1500        555         1000     8.88       FALSE
312         ABC123   10          555         1000     8.88       TRUE
123         ABC123   5           777         500      1.11       TRUE
312         ABC123   10          777         500      1.11       TRUE

For this scenario, of the 1500 I bought on Row 1, I only have a Total PO Quantity = 1000 for Maximum Price = $8.88. The remaining 500 I'll need to allocate to the lower PO_PRICE. So, I need to perform logic over the dataframe, and, the only way I can think to do it is to iterate over it. In this example, my result set ideally would be:

SO_NUMBER   ITEM #   SALES_QTY   PO_NUMBER   PO_QTY   PO_PRICE
--------------------------------------------------------------
123         ABC123   1000        555         1000     8.88    
123         ABC123   500         555         1000     8.88    
312         ABC123   0           555         1000     8.88    
123         ABC123   0           777         500      1.11    
312         ABC123   0           777         500      1.11 

As you can see, the items on the other Sales Orders, I can't report those as being valid, since I can't sell more than I bought, so, those need to get excluded.

I have the logic I wrote and it's fairly convoluted, but I've made great progress. Where I'm getting stuck now is that as I'm iterating over the rows, my logic tells me I need to delete certain rows, and, I don't think it's possible to delete while iterating and still performing the logic checks.

If anyone has any ideas how to help, I would greatly appreciate it. Like I said, I know my approach is wrong, but, I'm too new to Python and I can't think of any other way to do it.

Thanks in advance for any help.

来源:https://stackoverflow.com/questions/64462898/struggling-to-remove-rows-from-my-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!