Skip to content Skip to sidebar Skip to footer

Raise Valueerror('series Lengths Must Match To Compare') While Manipulating Dataframe

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

Solution 1:

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

Post a Comment for "Raise Valueerror('series Lengths Must Match To Compare') While Manipulating Dataframe"