我一直试图使用DataFrame方法将一个新系列添加到我的.loc中,如下所示
EM['Starting'] = EM.loc[(['Program_Start_Date']==Trimester_Start_Date)]
然而,我收到了下面的错误,并没有能够找到如何修复它。
KeyError: 'False: boolean label can not be used without a boolean index'
数据看起来是这样的,我有三个月的日期,等于2022-05-30,所以我应该得到结果。
Program_Start_Date Course_Withdrawn_Date Program_Withdrawal_Date
0 2022-05-30 None None
1 2022-05-30 None None
2 2022-05-30 None None
3 2022-05-30 None None
4 2022-05-30 None None
.. ... ... ...
543 2021-02-01 None None
544 2021-02-01 None None
545 2019-05-27 2022-08-18 None
546 2019-05-27 2022-08-18 None
547 2019-05-27 2022-08-18 None
我以为我会得到一个简单的真实..。等
作为我的结果,并尝试了几种不同的方法,创建了一个系列,但到目前为止,没有一种方法起作用。
发布于 2022-11-01 04:18:53
因此,您尝试使用带有条件的.loc()
,因此它将返回True或False。除非索引也是True/False,否则这对.loc()
不起作用。
我认为您可能要做的是过滤dataframe,它看起来类似于:
filter = [x==y for x,y in zip(EM.Program_Start_Date, EM.Trimester_Start_Date)]
EM_new = EM[filter]
"filter“变量是传递给EM的True/False列表,以选择哪些行使其进入EM_new。
希望这能帮上忙!如果是的话,请投票接受我的解决方案。=D
https://stackoverflow.com/questions/74271184
复制相似问题