在处理数据框时引发ValueError('系列长度必须匹配才能进行比较') [英] raise ValueError('Series lengths must match to compare') while manipulating dataframe

查看:576
本文介绍了在处理数据框时引发ValueError('系列长度必须匹配才能进行比较')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的初学者,并且正在编写代码来操纵数据框以聚合位于两个值之间的行.该值是从数据框本身中的一个值中提取的.

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.

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

我使用a和a + 0.4作为两个界限来获取所有行.

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

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:

就像我之前说过的那样,我对python还是很陌生,所以我不太了解数据类型如何相互配合.似乎a也是Series.

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 a is Series as well.

如何实现我要执行的操作?

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

推荐答案

我认为您只需要为返回标量a除去[],否则输出为Series:

I think you need remove [] only for return scalar a, else output is 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

这篇关于在处理数据框时引发ValueError('系列长度必须匹配才能进行比较')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆