Pandas Dataframes:列表中某列的值是否嵌套在同一行的另一列中?

Pandas Dataframes: is the value of a column in a list nested in another column, same row?

我正在使用类似于以下内容的 Pandas 数据框:

     My_date      Something_else    My_list
0    25/10/2019   ...               [25/10/2019, 26/10/2019]
1    03/07/2019   ...               [28/11/2017, 12/12/2017, 26/12/2017]
2    09/04/2019   ...               [11/06/2015]

我想检查名为“My_date”的列中的值是否在同一行的列表中,列“” My_list”。例如,在这里我想获得以下输出,vectoriallyvery efficiently:

     Result
0    true
1    false
2    false

我可以使用“for”循环来做到这一点,描述了各种方法 here for instance. However, I am aware that iterating is rarely the best solution,更何况我的 table 有超过 1百万行和许多列表有 365 个值。 (但如上所示,这些列表并不总是 日期范围 。)

我知道有很多方法可以对 DataFrame 进行矢量计算,例如使用 .loc 或 .eval 。关键是,在我的例子中,由于这些嵌套列表,没有任何工作按预期进行......因此,我想 找到一个矢量化解决方案 来做到这一点。如果重要的话,我所有的“日期”都是pandas.Timestamp.

类型

可能还有其他与类似问题相关的问题,但是我没有找到任何合适的答案或问题用我自己的话。感谢您的帮助!

尝试:

df['Result'] = df.apply(lambda x: x['My_date'] in x['My_list'], axis=1)

df=pd.DataFrame({'My_date' : ['25/10/2019','03/07/2019','09/04/2019'], 'My_list' : [['25/10/2019', '26/10/2019'],['28/11/2017', '12/12/2017', '26/12/2017'],['11/06/2015']]})
df['Result'] = df.apply(lambda x: x['My_date'] in x['My_list'], axis=1)

输出:

      My_date                               My_list  Result
0  25/10/2019              [25/10/2019, 26/10/2019]   True
1  03/07/2019  [28/11/2017, 12/12/2017, 26/12/2017]  False
2  09/04/2019                          [11/06/2015]  False