'Inputting x,y,z value from dataset and grouping them into 2 groups (1 or 2)
I am working on a project with a dataset where columns x,y,z are coordinate. For example x,y,z could equal (1,0,35) or (49,23,5). I want to group them, g1 and g2, for when 25 < x < 49, 12< Y < 23, 15 < Z < 35. Then I want to use input() for x,y,z coordinate and the output will tell me which group the coordinate belongs to.
x = pd.DataFrame(dataset, columns= ['x'])
Y = pd.DataFrame(dataset, columns= ['y'])
Z = pd.DataFrame(dataset, columns= ['z'])
g1 = 1
g2 = 2
if 25 < x < 49:
print(g2)
else:
print(g1)
if 12< Y < 23:
print(g2)
else:
print(g1)
if 15 < Z < 35:
print(g2)
else:
print(g1)
Solution 1:[1]
You must enter the following line: x=1,2,3 The letters will be x or y or z, any number, but separated by commas. Next, the letter is the name of the column in the date frame, and a list is created from the numbers to transfer it to the dataframe. There should be three numbers. Variable 'how_many', specifies how many digits should fall in the range.
import pandas as pd
df = pd.DataFrame({'x': [1, 0, 35], 'y': [49, 23, 5], 'z': [51, 2, 10]})
xxx = (input())#Input string must be the following: x=1,2,3
col = xxx[0]#Column name
index = xxx.find("=") + 1#Find the next index after the = sign, this will be the list itself (comma-separated integers)
xxx = xxx[index:].split(',')#Creating a list
xxx = [int(item) for item in xxx]#Converting values to type int
g1 = 1
g2 = 2
how_many = 1 #How many numbers should fall into the range
if col == 'x':
df['x'] = xxx
if len(df[(25 < df['x']) & (df['x'] < 49)]['x']) >= how_many:
print(g2)
else:
print(g1)
if col == 'y':
df['y'] = xxx
if len(df[(12 < df['y']) & (df['y'] < 23)]['y']) >= how_many:
print(g2)
else:
print(g1)
if col == 'z':
df['z'] = xxx
if len(df[(15 < df['z']) & (df['z'] < 35)]['z']) >= how_many:
print(g2)
else:
print(g1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | inquirer |
