这是输入数据
仅在有期初存货价值的情况下,通过添加期初存货,购买数量和已售出数量来计算期末存货价值。
我想在有条件的情况下为“期初库存”和“平仓库存”添加值。
对于我来说,是df.iterrows()中的行:
df['Opening Stock'] = np.where((df['Site'] == df['Site'].shift(1)) & (df['Item Code'] == df['Item Code'].shift(1))& ((df['Opening Stock'] == 0) | (df['Opening Stock'].isna())),df['Closing Stock'].shift(1),df['Opening Stock'])
df['Closing Stock'][i] = df['Opening Stock'][i]+df['Purchase Qty'][i]+df['Sold Qty'][i]
这是输出的样子
问题在于数据集的大小很大,需要数小时才能完成。
有没有一种方法可以优化此代码?
答案 0 :(得分:1)
您可以执行此操作而无需任何迭代方法。第一步是将0
中的Opening Stock
值转换为np.nan
,以便我们在下一步中填充它们。
import pandas as pd
import numpy as np
df = pd.DataFrame({'Site': ['site 1', 'site 1', 'site 2', 'site 2'],
'Item Code': ['A', 'A', 'A', 'A'],
'Opening Stock': [1000, 0, 2000, 0],
'Closing Stock': [1200, 0, 2250, 0],
'Purchase Qty': [500, 100, 400, 300],
'Sold Qty': [-300, -200, -150, -100]})
df.loc[df['Opening Stock'] == 0, 'Opening Stock'] = np.nan
df['Opening Stock'] = df.groupby(['Site', 'Item Code'])['Opening Stock'].fillna(df['Closing Stock'].shift(1))
df['Closing Stock'] = df['Opening Stock'] + df['Purchase Qty'] + df['Sold Qty']
答案 1 :(得分:0)
将条件应用于Pandas数据框中的每一行的一种方法是df.apply():
df.apply(
self.my_function
args = function_args
....
)
def my_function(self, row, args):
row['Closing Stock']= row['Opening Stock']+row['Purchase Qty']+row['Sold Qty']
# you can do pretty much whatever you want inside this function
#note: row is a pandas series
return row #it return the row modified and inserts it into the dataframe