我有两个数据集。
df
Name Date Quantity
ZMTD 2018-06-30 1000
ZMTD 2018-05-31 975
ZMTD 2018-04-30 920
ZMTD 2018-03-30 900
ZMTD 2018-02-28 840
ZMTD 2018-01-31 820
ZMTD 2017-12-30 760
ZMTD 2017-11-31 600
ZMTD 2017-10-30 1200
ZMTD 2017-09-31 1170
ZMTD 2017-08-30 1090
ZMTD 2017-07-30 1100
df2
Name Date Factor
KOC 2018-01-15 0.5
ZMTD 2017-11-10 1.5
ZMTD 2018-03-20 2.5
BND 2016-03-20 25
我正在尝试在满足条件df ['Date'] 我写了以下代码 运行此代码时,我期望以下值 但是我得到的值是“数量”列除以最近的“因子”列值2.5,而不是最初除以1.5的值 我想知道我们是否可以保存初始迭代的值,然后使用迭代对先前的值运行新的迭代。name = df['Name'].iloc[0]
for i, row in df2.iterrows():
if row[0] == name:
factor_date = row[1]
ratio = row[2]
for j, rows in df.iterrows():
new_quantity = rows[2]
if (rows[1] < factor_date):
new_quantity = (new_quantity / ratio)
df.at[i, 'Quantity'] = new_quantity
Name Date Quantity
ZMTD 2018-06-30 1000
ZMTD 2018-05-31 975
ZMTD 2018-04-30 920
ZMTD 2018-03-30 900
ZMTD 2018-02-28 336
ZMTD 2018-01-31 328
ZMTD 2017-12-30 304
ZMTD 2017-11-31 240
ZMTD 2017-10-30 320
ZMTD 2017-09-31 312
ZMTD 2017-08-30 290.66
ZMTD 2017-07-30 293.34
答案 0 :(得分:2)
这将为您提供所需的内容:
df = df1.merge(df2, on='Name', how='left', suffixes=('', '2'))
df['Factor'] = ((df['Date'] < df['Date2']).astype(int) * df['Factor']).replace(0, 1)
df = df.groupby(['Name', 'Date']).agg({'Quantity': 'max', 'Factor': 'prod'}).reset_index()
df['Quantity'] = df['Quantity'] / df['Factor']
df[['Name', 'Date', 'Quantity']].sort_values(['Name', 'Date'], ascending=False).reset_index(drop=True)
# Name Date Quantity
#0 ZMTD 2018-06-30 1000.000000
#1 ZMTD 2018-05-31 975.000000
#2 ZMTD 2018-04-30 920.000000
#3 ZMTD 2018-03-30 900.000000
#4 ZMTD 2018-02-28 336.000000
#5 ZMTD 2018-01-31 328.000000
#6 ZMTD 2017-12-30 304.000000
#7 ZMTD 2017-11-31 240.000000
#8 ZMTD 2017-10-30 320.000000
#9 ZMTD 2017-09-31 312.000000
#10 ZMTD 2017-08-30 290.666667
#11 ZMTD 2017-07-30 293.333333