pandas 在操作数据帧时引发 ValueError('Series length must match to compare')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41026974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:35:17  来源:igfitidea点击:

raise ValueError('Series lengths must match to compare') while manipulating dataframe

pythonpandas

提问by GothamCityRises

I'm a python beginner, and was writing code to manipulate a dataframe to aggregate rows that lie between two values. The value is extracted from one of the values in the dataframe itself.

我是一名 Python 初学者,正在编写代码来操作数据框以聚合位于两个值之间的行。该值是从数据帧本身的值之一中提取的。

a = df_gb1.loc[0,['TimeStamp']]

And i'm using a and a+0.4 as the two bounds to get all the rows.

我使用 a 和 a+0.4 作为获取所有行的两个边界。

df_gb1[df_gb1['TimeStamp'].between(a-0,a+0.4, inclusive=True)]

I don't know a better way to implement this, but it still gives me this error:

我不知道更好的方法来实现这一点,但它仍然给我这个错误:

error

错误

Like I said before, I'm very new to python, so I don't quite know how datatypes work with one another. It seems as if ais Series as well.

就像我之前说的,我对 python 很陌生,所以我不太了解数据类型如何相互配合。好像a也是系列。

How do I implement what I'm trying to do?

我如何实施我正在尝试做的事情?

采纳答案by jezrael

I think you need remove []only for return scalar a, else output is Series:

我认为你[]只需要删除返回标量a,否则输出是Series

df_gb1 = pd.DataFrame({'TimeStamp':[3,3.1,5,7.1,3.4],
                   'a':[4,5,6,7,8]})

print (df_gb1)
   TimeStamp  a
0        3.0  4
1        3.1  5
2        5.0  6
3        7.1  7
4        3.4  8

a = df_gb1.loc[0,'TimeStamp']
print (a)
3.0

print (df_gb1[df_gb1['TimeStamp'].between(a-0,a+0.4, inclusive=True)])
   TimeStamp  a
0        3.0  4
1        3.1  5
4        3.4  8


print (df_gb1.loc[0,['TimeStamp']])
TimeStamp    3.0
Name: 0, dtype: float64