[英]Excel VLOOKUP equivalent in pandas
I have following dataframe:我有以下数据框:
A B C
Index
2001-06-30 100 2001-08-31 (=value of A at date B)
2001-07-31 200 2001-09-30 ...
2001-08-31 300 2001-10-31 ...
2001-09-30 400 2001-11-30 ...
Column B
consists of dates from column A
shifted forward by some. B
列包含A
列中向前移动了一些的日期。 I would like to generate column C
that consists of the values from column A
on date B
.我想生成由日期
B
上A
列的值组成的C
列。 (preferably in the logic the Excel VLOOKUP formula would do it. I am not looking for simply shift(-2)
here because in reality the shift between B
and Index
is not always equal). (最好在逻辑中 Excel VLOOKUP 公式会做到这一点。我不是在这里寻找简单的
shift(-2)
,因为实际上B
和Index
之间的转换并不总是相等的)。
I tried df.loc['B', 'A']
but this most probably too simplistic and produced an error.我试过
df.loc['B', 'A']
但这很可能太简单了并产生了错误。
I think you need map
by column A
:我认为您需要按
A
列map
:
df['C'] = df.B.map(df.A)
print (df)
A B C
Index
2001-06-30 100 2001-08-31 300.0
2001-07-31 200 2001-09-30 400.0
2001-08-31 300 2001-10-31 NaN
2001-09-30 400 2001-11-30 NaN
It is same as:它与:
df['C'] = df.B.map(df.A.to_dict())
print (df)
A B C
Index
2001-06-30 100 2001-08-31 300.0
2001-07-31 200 2001-09-30 400.0
2001-08-31 300 2001-10-31 NaN
2001-09-30 400 2001-11-30 NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.