从值的数据框返回列表,其日期与列表中的日期匹配

时间:2017-07-17 14:56:27

标签: python pandas dataframe

所以我有一个pandas数据帧df_testing_set,看起来像这个样本:

Index Ycurrent.   date.   bucket_id.   
 .     245    June 17, 2017.   45
 .     235    June 17, 2017.   46
 .     265    June 18, 2017.   47
 .     235    June 18, 2017.   48
 .     225    June 19, 2017.   49
 .     205    June 20, 2017.   50
 .     215    June 21, 2017.   51
 .     212    June 22, 2017.   52
 .     225    June 23, 2017.   53
 .     257    June 24, 2017.   54
 .     236    June 25, 2017.   55
 .     248    June 26, 2017.   56
 .     245    June 27, 2017.   57
 .     245    June 27, 2017.   58

我有一个包含来自另一个数据框的8个随机日期的列表,如下所示:

0.   June 01, 2017
1.   June 23, 2017
2.   June 13, 2017
3.   June 27, 2017
4.   June 17, 2017
5.   June 04, 2017
6.   June 09, 2017
7.   June 11, 2017
8.   June 15, 2017

鉴于上述数据,我如何(对于date_list中的每个日期),选择该特定日期的所有记录(从我的代码看起来每个日期约有144行)

根据这些数据,我一直试图得到(x,y)其中xbucket_id中的值(从1-144开始),y是该字段中的值Ycurrent。然后将坐标与matplotlib一起使用以绘制折线图。

当我尝试使用matplotlib进行绘图时,我的图表不显示。我试图在同一个图表上绘制所有线条,因为所有日期的x轴保持不变,但我一直在

raise ValueError('Must pass DataFrame with boolean values only')

ValueError: Must pass DataFrame with boolean values only

1 个答案:

答案 0 :(得分:1)

IIUC,您可以使用isin

过滤原始数据框
df_testing_set = df_testing_set[df_testing_set['date'].isin(date_list[1])]

其中date_list[1]应该是与第二个数据框/日期列表的日期相关的列。

如果您希望第一个索引只是选择它:

df_testing_set = df_testing_set[df_testing_set['date'].isin(date_list[1])]['Index']

希望有所帮助。