范围查找过滤之间的Pandas

时间:2017-05-17 16:18:20

标签: python python-3.x pandas

我的数据如下:

x

如何过滤y值(符合x_range内部值)和z值以返回适当的x值?例如,如果y = 248且Fish = 5,我想返回#!/usr/bin/env python import socket TCP_IP = '127.0.0.1' TCP_PORT = 5005 BUFFER_SIZE = 20 # Normally 1024, but we want fast response s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((TCP_IP, TCP_PORT)) s.listen(1) conn, addr = s.accept() print 'Connection address:', addr while 1: data = conn.recv(BUFFER_SIZE) if not data: break print "received data:", data conn.send(data) # echo conn.close() ...

2 个答案:

答案 0 :(得分:1)

简单过滤练习:

保存您的数据框:

df['x_range_start'] = [int(i.split('-')[0]) for i in df.x_range]

为范围开始和结束添加两列:

df['x_range_start'] = [int(i.split('-')[0]) for i in df.x_range]
df['x_range_end'] = [int(i.split('-')[1]) for i in df.x_range]

过滤以查找值:

x_value = 113
y_value = 5

df[(df.x_range_start <= x_value) &(x_value <= df.x_range_end)][df.y == y_value]['z']

答案 1 :(得分:0)

我不喜欢这个,但我一起解决了一个问题:

分裂:

df['low'], df['high'] = df['x_range'].str.split('-', 1).str
df['low'] = pd.to_numeric(df['low'])
df['high'] = pd.to_numeric(df['high'])

过滤器:

x = 248
y = 6
row = df[(pd.Series.between(x, left=df.low, right=df.high, inclusive=True)) & 
(df.y == y)]
row['z']