Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按日期索引上的条件筛选数据帧_Python_Pandas_Date_Datetime_Dataframe - Fatal编程技术网

Python 按日期索引上的条件筛选数据帧

Python 按日期索引上的条件筛选数据帧,python,pandas,date,datetime,dataframe,Python,Pandas,Date,Datetime,Dataframe,我用frame['date\u created']创建了一个带有值的熊猫系列。value\u counts()。sort\u index() 但我想过滤这个系列,以获得2017年及以上日期的数据。如何过滤此内容?str[…]切片 很简单,如果您处理的是字符串索引,那么切片、转换和比较: v = frame['date_created'].value_counts().sort_index() v_2017 = v[v.index.str[:4].astype(int) >= 2017]

我用
frame['date\u created']创建了一个带有值的熊猫系列。value\u counts()。sort\u index()

但我想过滤这个系列,以获得2017年及以上日期的数据。如何过滤此内容?

str[…]
切片 很简单,如果您处理的是字符串索引,那么切片、转换和比较:

v = frame['date_created'].value_counts().sort_index()
v_2017 = v[v.index.str[:4].astype(int) >= 2017]


pd.to_datetime
或者,强制转换为datetime-

v_2017 = v[pd.to_datetime(v.index).year >= 2017]
print(v_2017)

2017-05     5
2017-07     2
2017-08    13
2017-09    40
2017-10    47
2017-11    40
2017-12    26
2018-01    16
Name: 1, dtype: int64
str[…]
切片 很简单,如果您处理的是字符串索引,那么切片、转换和比较:

v = frame['date_created'].value_counts().sort_index()
v_2017 = v[v.index.str[:4].astype(int) >= 2017]


pd.to_datetime
或者,强制转换为datetime-

v_2017 = v[pd.to_datetime(v.index).year >= 2017]
print(v_2017)

2017-05     5
2017-07     2
2017-08    13
2017-09    40
2017-10    47
2017-11    40
2017-12    26
2018-01    16
Name: 1, dtype: int64

这里有一种方法:

import pandas as pd

df = pd.DataFrame({'date_created': ['2013-10','2014-12',
                                    '2015-02','2015-03',
                                    '2015-09','2016-02',
                                    '2016-03','2017-05',
                                    '2017-07','2017-08',
                                    '2017-09','2017-10',
                                    '2017-11','2017-12',
                                    '2018-01'],
                    'count': [1, 1, 1, 1, 1, 6, 1, 5, 2, 13, 40, 47, 40, 26, 16]})

print(df[df['date_created'].apply(lambda x: int(x.split('-')[0])).gt(2016)])
#    count date_created
#7       5      2017-05
#8       2      2017-07
#9      13      2017-08
#10     40      2017-09
#11     47      2017-10
#12     40      2017-11
#13     26      2017-12
#14     16      2018-01

这里有一种方法:

import pandas as pd

df = pd.DataFrame({'date_created': ['2013-10','2014-12',
                                    '2015-02','2015-03',
                                    '2015-09','2016-02',
                                    '2016-03','2017-05',
                                    '2017-07','2017-08',
                                    '2017-09','2017-10',
                                    '2017-11','2017-12',
                                    '2018-01'],
                    'count': [1, 1, 1, 1, 1, 6, 1, 5, 2, 13, 40, 47, 40, 26, 16]})

print(df[df['date_created'].apply(lambda x: int(x.split('-')[0])).gt(2016)])
#    count date_created
#7       5      2017-05
#8       2      2017-07
#9      13      2017-08
#10     40      2017-09
#11     47      2017-10
#12     40      2017-11
#13     26      2017-12
#14     16      2018-01