我有一个这样的数据框:
category date number
0 Cat1 2010-03-01 1
1 Cat2 2010-09-01 1
2 Cat3 2010-10-01 1
3 Cat4 2010-12-01 1
4 Cat5 2012-04-01 1
5 Cat2 2013-02-01 1
6 Cat3 2013-07-01 1
7 Cat4 2013-11-01 2
8 Cat5 2014-11-01 5
9 Cat2 2015-01-01 1
10 Cat3 2015-03-01 1
我想检查此数据框中是否存在日期,但无法。我尝试了以下各种方法,但仍然没有用:
if pandas.Timestamp("2010-03-01 00:00:00", tz=None) in df['date'].values:
print 'date exist'
if datetime.strptime('2010-03-01', '%Y-%m-%d') in df['date'].values:
print 'date exist'
if '2010-03-01' in df['date'].values:
print 'date exist'
“日期存在”从未打印。我如何检查日期是否存在?因为我想在所有类别中插入数字等于0的“不存在”日期,以便可以绘制连续的折线图(每行一个类别)。感谢您的帮助。提前谢谢。
最后一个给我这个:
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
而date exist
没有打印出来。
例如,要确认
ds
的第4个值包含在其自身中:让
ds
成为一个Pandas数据系列,其格式为[index,Pandas.[u libs.tslib.Timestamp]并带有示例值:0 2018-01-31 19:08:27.465515 1 2018-02-01 19:08:27.465515 2 2018-02-02 19:08:27.465515 3 2018-02-03 19:08:27.465515 4 2018-02-04 19:08:27.465515
然后,我们使用
isin
本地方法获取布尔值的数据序列,其中每个条目指示ds
中的位置是否与作为参数传递给函数的值匹配(因为isin
需要一个值列表,我们需要以列表格式提供该值)。接下来,我们使用
set
全局方法获得一个具有1个或2个值的集合,这取决于是否存在匹配(真值和假值)或不匹配(仅为假值)。最后,我们检查集合是否包含1个以上的值,如果是这样,则意味着我们有一个匹配项,否则就没有匹配项。
我认为您需要首先通过^{} 转换为datetime,然后如果需要,请选择所有行,使用^{} :
对于返回值} 转换为
True
,使用由^{numpy array
的检查值:或者至少一个} 作为注释Edchum:
True
由^{相关问题 更多 >
编程相关推荐