问题描述
我正在从csv创建一个DataFrame,如下所示:
I am creating a DataFrame from a csv as follows:
stock = pd.read_csv('data_in/' + filename + '.csv', skipinitialspace=True)
DataFrame有一个日期列.有没有一种方法来创建一个新的DataFrame(或仅覆盖现有的DataFrame),该DataFrame仅包含日期值在指定日期范围内或两个指定日期值之间的行?
The DataFrame has a date column. Is there a way to create a new DataFrame (or just overwrite the existing one) which only contains rows with date values that fall within a specified date range or between two specified date values?
推荐答案
有两种可能的解决方案:
There are two possible solutions:
- 使用布尔型掩码,然后使用
df.loc[mask]
- 将日期列设置为DatetimeIndex,然后使用
df[start_date : end_date]
- Use a boolean mask, then use
df.loc[mask]
- Set the date column as a DatetimeIndex, then use
df[start_date : end_date]
使用布尔掩码:
确保df['date']
是dtype datetime64[ns]
的系列:
Ensure df['date']
is a Series with dtype datetime64[ns]
:
df['date'] = pd.to_datetime(df['date'])
制作布尔型蒙版. start_date
和end_date
可以是datetime.datetime
s,np.datetime64
s,pd.Timestamp
s甚至日期时间字符串:
Make a boolean mask. start_date
and end_date
can be datetime.datetime
s,np.datetime64
s, pd.Timestamp
s, or even datetime strings:
#greater than the start date and smaller than the end date
mask = (df['date'] > start_date) & (df['date'] <= end_date)
选择子DataFrame:
Select the sub-DataFrame:
df.loc[mask]
或重新分配给df
df = df.loc[mask]
例如,
For example,
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.random((200,3)))
df['date'] = pd.date_range('2000-1-1', periods=200, freq='D')
mask = (df['date'] > '2000-6-1') & (df['date'] <= '2000-6-10')
print(df.loc[mask])
收益
0 1 2 date
153 0.208875 0.727656 0.037787 2000-06-02
154 0.750800 0.776498 0.237716 2000-06-03
155 0.812008 0.127338 0.397240 2000-06-04
156 0.639937 0.207359 0.533527 2000-06-05
157 0.416998 0.845658 0.872826 2000-06-06
158 0.440069 0.338690 0.847545 2000-06-07
159 0.202354 0.624833 0.740254 2000-06-08
160 0.465746 0.080888 0.155452 2000-06-09
161 0.858232 0.190321 0.432574 2000-06-10
使用 DatetimeIndex :
Using a DatetimeIndex:
如果您要按日期进行很多选择,则设置快捷方式可能会更快date
列首先作为索引.然后您可以使用日期按日期选择行df.loc[start_date:end_date]
.
If you are going to do a lot of selections by date, it may be quicker to set thedate
column as the index first. Then you can select rows by date usingdf.loc[start_date:end_date]
.
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.random((200,3)))
df['date'] = pd.date_range('2000-1-1', periods=200, freq='D')
df = df.set_index(['date'])
print(df.loc['2000-6-1':'2000-6-10'])
收益
0 1 2
date
2000-06-01 0.040457 0.326594 0.492136 # <- includes start_date
2000-06-02 0.279323 0.877446 0.464523
2000-06-03 0.328068 0.837669 0.608559
2000-06-04 0.107959 0.678297 0.517435
2000-06-05 0.131555 0.418380 0.025725
2000-06-06 0.999961 0.619517 0.206108
2000-06-07 0.129270 0.024533 0.154769
2000-06-08 0.441010 0.741781 0.470402
2000-06-09 0.682101 0.375660 0.009916
2000-06-10 0.754488 0.352293 0.339337
同时使用Python列表索引,例如seq[start:end]
包括start
但不包括end
,相比之下,熊猫df.loc[start_date : end_date]
如果在索引中,则在结果中同时包含和两个端点.但是,start_date
和end_date
都不必在索引中.
While Python list indexing, e.g. seq[start:end]
includes start
but not end
, in contrast, Pandas df.loc[start_date : end_date]
includes both end-points in the result if they are in the index. Neither start_date
nor end_date
has to be in the index however.
还请注意, pd.read_csv
具有一个parse_dates
参数,可用于将date
列解析为datetime64
.因此,如果使用parse_dates
,则无需使用df['date'] = pd.to_datetime(df['date'])
.
Also note that pd.read_csv
has a parse_dates
parameter which you could use to parse the date
column as datetime64
s. Thus, if you use parse_dates
, you would not need to use df['date'] = pd.to_datetime(df['date'])
.
这篇关于选择两个日期之间的DataFrame行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!