簡體   English   中英

從數據集中輸入 x、y、z 值並將它們分為 2 組(1 或 2)

[英]Inputting x,y,z value from dataset and grouping them into 2 groups (1 or 2)

我正在處理一個項目,其中包含 x、y、z 列是坐標的數據集。 例如 x,y,z 可以等於 (1,0,35) 或 (49,23,5)。 我想將它們分組,g1 和 g2,當 25 < x < 49、12< Y < 23、15 < Z < 35 時。然后我想對 x、y、z 坐標使用 input(),output 會告訴我坐標屬於哪個組。

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)

您必須輸入以下行:x=1,2,3 字母將為 x 或 y 或 z,任意數字,但以逗號分隔。 接下來,字母是日期框中列的名稱,並根據數字創建一個列表以將其傳輸到 dataframe。應該有三個數字。 變量“how_many”,指定范圍內應包含多少位數字。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM