ValueError:系列长度必须匹配才能在 pandas 匹配日期时进行比较
问题说明
对于这样的基本问题,我事先表示歉意,但我很沮丧.
I apologize in advance for asking such a basic question but I am stumped.
这是一个非常简单的虚拟示例.我在Pandas中遇到一些匹配日期的问题,我不知道为什么.
This is a very simple, dummy example. I'm having some issue matching dates in Pandas and I can't figure out why.
df = pd.DataFrame([[1,'2016-01-01'],
[2,'2016-01-01'],
[3,'2016-01-02'],
[4,'2016-01-03']],
columns=['ID', 'Date'])
df['Date'] = df['Date'].astype('datetime64')
说我想匹配上述df中的第1行.
我事先知道我要匹配ID 1
.
而且我也知道我想要的日期,实际上,我将直接从df的第1行提取该日期,以使其防弹.
Say I want to match row 1 in the above df.
I know beforehand that I want to match ID 1
.
And I know the date I want as well, and as a matter of fact, I'll extract that date directly from row 1 of the df to make it bulletproof.
some_id = 1
some_date = df.iloc[1:2]['Date'] # gives 2016-01-01
那为什么为什么这一行不给我返回第1行?
So why doesn't this line work to return me row 1??
df[(df['ID']==some_id) & (df['Date'] == some_date)]
相反,我得到了 ValueError: Series lengths must match to compare
据我所知,这是有道理的...但是让我纳闷...如果我不能一个一个地比较,该如何在熊猫中比较日期?
Instead I get ValueError: Series lengths must match to compare
which I understand, and makes sense...but leaves me wondering...how else can I compare dates in pandas if I can't compare one to many?
正确答案
您说:
some_date = df.iloc[1:2]['Date'] # gives 2016-01-01
但这不是它所提供的.它为Series提供了一个元素,而不仅仅是一个值-当您使用[1:2]
作为切片时,您不会得到一个元素,而是一个包含一个元素的容器:
but that's not what it gives. It gives a Series with one element, not simply a value -- when you use [1:2]
as your slice, you don't get a single element, but a container with one element:
>>> some_date
1 2016-01-01
Name: Date, dtype: datetime64[ns]
相反,做
>>> some_date = df.iloc[1]['Date']
>>> some_date
Timestamp('2016-01-01 00:00:00')
之后
>>> df[(df['ID']==some_id) & (df['Date'] == some_date)]
ID Date
0 1 2016-01-01
(请注意,如果要查找很多some_id
和some_date
值,则模式会更有效,但这是一个单独的问题.)
(Note that there are more efficient patterns if you have a lot of some_id
and some_date
values to look up, but that's a separate issue.)
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcgchii
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20