Search code examples
pythondataframeelementdecrement

Struggling with basic dataframe manipulation (Python)


I am a complete newbie at Python, and I'm struggling despite having Googled for quite a while. I know it should not be this difficult. I have a Dataframe called abc that looks like this:

   PO_DATE      PO_ITEM_NUMBER     PO_PRICE       PO_QTY
----------------------------------------------------------------
0  01/15/2017   ABC123             1.55           1000
1  01/25/2017   DEF456             5.55           500

I know the max PO_PRICE = 5.55, which I can find using:

max_PO_Price = abc["PO_PRICE"].max()

All I want to be able to do is identify which row has the max PO_PRICE, locate the PO_QTY field for that row, and decrement it by 100. I keep wanting to envision this dataframe is a 2-D array, but, it's not liking that, and, I know it's not an array, as there are different data types involved. I've been screwing this up now for too long, so, finally decided to post. I hope someone can forgive my lack of knowledge and point me in the right direction.

Thanks.


Solution

  • I think you want:

    abc.loc[abc.PO_PRICE == abc["PO_PRICE"].max(), ['PO_QTY']] = abc['PO_QTY']-100

    An example below:

    # Import pandas
    import pandas as pd
    
    # Create dataframe from example and priunt
    abc = pd.DataFrame({'PO_Date':['01/15/2017', '01/25/2017'], 'PO_ITEM_NUMBER': ['ABC123', 'DEF456'], 'PO_PRICE':[1.55, 5.55], 'PO_QTY':[1000, 500]})
    print(abc)
    
    # Find which row has the max PO Price, and then subtract the quantity by 100
    abc.loc[abc.PO_PRICE == abc['PO_PRICE'].max(), ['PO_QTY']] = abc['PO_QTY']-100
    
    # Print it out
    print(abc)
    

    Yields:

          PO_Date PO_ITEM_NUMBER  PO_PRICE  PO_QTY
    0  01/15/2017         ABC123      1.55    1000
    1  01/25/2017         DEF456      5.55     500
    
          PO_Date PO_ITEM_NUMBER  PO_PRICE  PO_QTY
    0  01/15/2017         ABC123      1.55    1000
    1  01/25/2017         DEF456      5.55     400
    

    You should consider looking at this post, which my answer was influenced from. Additionally, the documentation on .loc might be helpful.