给定一个DataFrame,索引是Datetime,如何通过仅匹配Date部分来检索行?
例如:
df1 =
A B C D
2011-01-13 16:00:00 344 144 616 73
2011-01-14 16:00:00 346 145 624 74
2011-01-18 16:00:00 339 146 639 77
...
并给出:
df2['Date'] =
0 2011-01-13
1 2011-01-13
2 2011-01-26
3 2011-02-02
4 2011-02-10
5 2011-03-03
6 2011-03-03
7 2011-06-03
8 2011-05-03
9 2011-06-10
10 2011-08-01
11 2011-08-01
12 2011-12-20
我想要这样的事情:
for indx, row in df2.iterrows():
print df1.loc[df1.index.date() == row['Date'].date()]
答案 0 :(得分:3)
print df1.index.to_series().dt.date
2011-01-13 16:00:00 2011-01-13
2011-01-14 16:00:00 2011-01-14
2011-01-18 16:00:00 2011-01-18
dtype: object
print df1.index.to_series().dt.date.isin(df2['Date'].dt.date)
Name: Date, dtype: object
2011-01-13 16:00:00 True
2011-01-14 16:00:00 False
2011-01-18 16:00:00 False
dtype: bool
print df1[df1.index.to_series().dt.date.isin(df2['Date'].dt.date)]
A B C D
2011-01-13 16:00:00 344 144 616 73
或许你需要:
print df1.index.date
[datetime.date(2011, 1, 13) datetime.date(2011, 1, 14)
datetime.date(2011, 1, 18)]
print df2['Date'].dt.date.isin(df1.index.date)
0 True
1 True
2 False
3 False
4 False
5 False
6 False
7 False
8 False
9 False
10 False
11 False
12 False
Name: Date, dtype: bool
print df2[df2['Date'].dt.date.isin(df1.index.date)]
Date
0 2011-01-13
1 2011-01-13
答案 1 :(得分:0)
您可以在df1
中创建日期列,然后将该表合并到df2
(假设df2
中的日期属于datetime.date类型):
df1['Date'] = [d.date() for d in df1.index]
>>> df2.merge(df1, how='left', on='Date']
Date A B C D
0 2011-01-13 344 144 616 73
1 2011-01-13 344 144 616 73
2 2011-01-26 NaN NaN NaN NaN
3 2011-02-02 NaN NaN NaN NaN
4 2011-02-10 NaN NaN NaN NaN
5 2011-03-03 NaN NaN NaN NaN
6 2011-03-03 NaN NaN NaN NaN
7 2011-06-03 NaN NaN NaN NaN
8 2011-05-03 NaN NaN NaN NaN
9 2011-06-10 NaN NaN NaN NaN
10 2011-08-01 NaN NaN NaN NaN
11 2011-08-01 NaN NaN NaN NaN
12 2011-12-20 NaN NaN NaN NaN