简体   繁体   English

从上一行访问值 pandas dataframe

[英]Access value from previous row pandas dataframe

I am currently writing some code to get stock quantities, and the code depends on whether items are moved from somewhere or to somewhere first.我目前正在编写一些代码来获取库存数量,该代码取决于物品是先从某处移动还是先移动到某处。 Therefore I need the value for the column in the dataframe which determines which action has been carried out (stock counted, stock moved, stock used etc.) from the previous row.因此,我需要 dataframe 中列的值,该值确定从上一行执行了哪些操作(库存盘点、库存移动、使用的库存等)。

I have tried using df.shift() as I saw that in answer to another question, but that returns the entire column.我曾尝试使用df.shift() ,因为我在回答另一个问题时看到了这一点,但这会返回整个列。 Also I need it to be a string, I am hoping that the conversion will be possible using str() but I haven't been able to try it yet.我还需要它是一个字符串,我希望可以使用str()进行转换,但我还没有尝试过。

My current, simplified code is:我当前的简化代码是:

if row['Action'] == "Move From":
    if str(df['Action'].shift(1)) == "Move To": # Have tried with and without 1 argument
        # Rest of code
        print('Shift worked')

A sample df format would be something like:示例 df 格式将类似于:

Stock Location, Action, Total Quantity, Location Quantity
A1, Stock Count, 500, 500
A1, Move From, 500, 250
A2, Move To, 500, 250
A2, Stock Count, 500, 250
A1, Stock Count, 500, 250

Where the Total Quantity never changes because the stock is moving, but the quantities of the stock in locations A1 and A2 do change.总数量永远不会因为库存移动而发生变化,但位置 A1 和 A2 的库存数量确实发生了变化。 The issue is that Move To doesn't always follow Move From , sometimes they are the other way around.问题是Move To并不总是跟随Move From ,有时它们是相反的。 To get the Factory Quantity, I sum up the quantities of all the locations which stock is stored in. I have been creating a Temp_Var equal to either the amount of stock that is moving, or the corresponding negative amount, depending on whether stock is moving from or to.为了获得工厂数量,我总结了存储库存的所有位置的数量。我一直在创建一个Temp_Var ,它等于正在移动的库存量或相应的负数,具体取决于库存是否正在移动从或到。 However, the Temp_Var is only required for the first move, if the stock has already been moved the Temp_Var is not needed.但是, Temp_Var仅在第一次移动时需要,如果库存已经移动,则不需要Temp_Var

For additional clarity:为了更加清楚:

Using the above dataframe, during row 3, Total Quantity would equal the quantity in row A2 and an additional 250 in a temporary variable (as we don't yet know where it is moving to), then during row 4, Total Quantity would equal the quantities in row A1 and A2, not needing the temporary variable.使用上面的 dataframe,在第 3 行期间, Total Quantity将等于 A2 行中的数量和临时变量中的额外 250(因为我们还不知道它移动到哪里),然后在第 4 行期间, Total Quantity将等于A1 和 A2 行中的数量,不需要临时变量。 Because sometimes Move To comes first, just by checking the 'Action' , I don't always know if I'll need a temp variable.因为有时Move To先出现,仅通过检查'Action' ,我并不总是知道是否需要临时变量。

Realistically though the reasoning behind this is largely superfluous and makes the actually problem seem more confusing than it is.实际上,尽管这背后的推理在很大程度上是多余的,并且使实际问题看起来比实际更令人困惑。 I just need to be able to check a value from the previous row我只需要能够检查上一行的值

df['Action'].shift(1) creates a new column, so you need to do it before iterating through the dataframe, assigning it a new name: df['Action'].shift(1)创建一个新列,因此您需要在遍历 dataframe 之前执行此操作,并为其分配一个新名称:

df['Previous Action'] = df['Action'].shift(1) , and then refer to new column during iteration. df['Previous Action'] = df['Action'].shift(1) ,然后在迭代过程中引用新列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM