I'm trying set a upper bound and lower bound based out of user input where upper bound is userinput 10
create DataFrame
df = pd.DataFrame({
'VIN':['v1', 'v1', 'v1', 'v1', 'v1', 'v2', 'v2', 'v2', 'v2', 'v2'],
'Revenue':[30, 50, 40, 30, 40, 30, 50, 40, 30, 50],
'Category':['MKL', 'GKL','GKL', 'UKL', 'MKL', 'MKL', 'GKL', 'GKL', 'UKL', 'MKL'],
'Age':[64, 41, 54, 63, 64, 63, 53, 42, 32, 61]
})
print(df)
VIN Revenue Category Age
0 v1 30 MKL 64
1 v1 50 GKL 41
2 v1 40 GKL 54
3 v1 30 UKL 63
4 v1 40 MKL 64
5 v2 30 MKL 63
6 v2 50 GKL 53
7 v2 40 GKL 42
8 v2 30 UKL 32
9 v2 50 MKL 61
i tried below code but getting error of : ('Lengths must match to compare', (995,), (1,)):
Age1 = [int(x) for x in input('Please enter Age?= ').split(',')]
dummy=[10]
t2=[x y for x, y in zip(Age1, dummy)]
df = df[(df['Age'] >= Age1) & (df['Age'] <= t2)]
CodePudding user response:
The problem is storing the values in lists and accessing these, after changing the query to access the list value through Age1[0] (the first and only value in the list), the value is correctly stored. Your zip function also ceased working as you need to include the columns in square brackets like [Age1]. In each case you should specify to take the first value from the list you are storing the results in using [0].
The code works for me with the following additions:
import pandas as pd
df = pd.DataFrame({
'VIN':['v1', 'v1', 'v1', 'v1', 'v1', 'v2', 'v2', 'v2', 'v2', 'v2'],
'Revenue':[30, 50, 40, 30, 40, 30, 50, 40, 30, 50],
'Category':['MKL', 'GKL','GKL', 'UKL', 'MKL', 'MKL', 'GKL', 'GKL', 'UKL', 'MKL'],
'Age':[64, 41, 54, 63, 64, 63, 53, 42, 32, 61]
})
Age1 = [int(x) for x in input('Please enter Age?= ').split(',')]
dummy=[10]
t2=[x y for x, y in zip([Age1[0]], [dummy[0]])]
df = df[(df['Age'] >= Age1[0]) & (df['Age'] <= t2[0])]
print(Age1)
print(df)
[60]
VIN Revenue Category Age
0 v1 30 MKL 64
3 v1 30 UKL 63
4 v1 40 MKL 64
5 v2 30 MKL 63
9 v2 50 MKL 61