根据日期创建两个新列

时间:2019-04-16 13:51:16

标签: python pandas list list-comprehension

我遇到了以下问题。我想通过检查日期/索引将两列合并为一列。表结构如下:

Date          A     B
01/02/2019  93500   27500
02/02/2019  93500   27500
03/02/2019  93500   27500
04/02/2019  93500   27500
05/02/2019  93300   26900
06/02/2019  93300   26900
07/02/2019  93300   26900
08/02/2019  93300   26900
09/02/2019  93300   26700
10/02/2019  93300   26700
11/02/2019  93300   26700
12/02/2019  93300   26700
13/02/2019  93100   26300



df['C'] = [df.A if df[df.index < "2019-02-08"] else df.B for row in df.index]

这应该是结果:

Date          A     B       C
01/02/2019  93500   27500   93500
02/02/2019  93500   27500   93500
03/02/2019  93500   27500   93500
04/02/2019  93500   27500   93500
05/02/2019  93300   26900   93300
06/02/2019  93300   26900   93300
07/02/2019  93300   26900   93300
08/02/2019  93300   26900   93300
09/02/2019  93300   26700   26700
10/02/2019  93300   26700   26700
11/02/2019  93300   26700   26700
12/02/2019  93300   26700   26700
13/02/2019  93100   26300   26300

但是我收到以下错误:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

非常感谢您的帮助!

0 个答案:

没有答案
相关问题