Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 搜索输入值小于下一个索引列值的dataframe列_Python_Pandas - Fatal编程技术网

Python 搜索输入值小于下一个索引列值的dataframe列

Python 搜索输入值小于下一个索引列值的dataframe列,python,pandas,Python,Pandas,我想搜索一个数据框,找到一个对应于索引行和列值的值。但是,我遇到了困难,因为列标题值是一个范围的上限,其中前一个列标题值是下限(但它的另一个neightbor列值是上限) 我找不到将输入与对应于范围下限的列相匹配的方法 通过一个例子,很容易看出: data_frame_test = pd.DataFrame({'location' : [1, 2, 'S'], '200' : [342, 690, 103],

我想搜索一个数据框,找到一个对应于索引行和列值的值。但是,我遇到了困难,因为列标题值是一个范围的上限,其中前一个列标题值是下限(但它的另一个neightbor列值是上限)

我找不到将输入与对应于范围下限的列相匹配的方法

通过一个例子,很容易看出:

data_frame_test = pd.DataFrame({'location' : [1, 2, 'S'],
                                '200' : [342, 690, 103],
                                '1000' : [322, 120, 193],
                                '2000' : [249, 990, 403]})

data_frame_test = data_frame_test.set_index('location')
我想做的是

location = 1
weight = 500

output = data_frame_test.iloc[1][??] #must be equal to 342

看,权重必须查看的列是200,因为它在[200]之间;1000[.我不知道还有什么可以尝试将其转换为python代码。如果需要任何帮助,将不胜感激。

首先通过
重命名将列转换为整数,如果需要:

data_frame_test = data_frame_test.set_index('location').rename(columns=int)
print (data_frame_test)
          200   1000  2000
location                  
1          342   322   249
2          690   120   990
S          103   193   403

weight = 500
location = 1
然后通过比较较小的值(如重量),将位置值与最后一个位置的
True
值进行匹配:

#https://stackoverflow.com/a/8768734
b = (data_frame_test.columns[::-1] < weight)
pos = len(b) - np.argmax(b) - 1
print (pos)
0

output = data_frame_test.loc[location, data_frame_test.columns[pos]]
print (output)
342

如有必要,首先通过重命名将列转换为整数:

data_frame_test = data_frame_test.set_index('location').rename(columns=int)
print (data_frame_test)
          200   1000  2000
location                  
1          342   322   249
2          690   120   990
S          103   193   403

weight = 500
location = 1
然后通过比较较小的值(如重量),将位置值与最后一个位置的
True
值进行匹配:

#https://stackoverflow.com/a/8768734
b = (data_frame_test.columns[::-1] < weight)
pos = len(b) - np.argmax(b) - 1
print (pos)
0

output = data_frame_test.loc[location, data_frame_test.columns[pos]]
print (output)
342

我可以考虑将列扫描到
int
,并使用和布尔索引对数据帧进行索引:

location = 1
weight = 500 

data_frame_test.iloc[data_frame_test.index==location, 
                     data_frame_test.columns.astype('int').searchsorted(weight)-1]

location
1    342
Name: 200, dtype: int64

我可以考虑将列扫描到
int
,并使用和布尔索引对数据帧进行索引:

location = 1
weight = 500 

data_frame_test.iloc[data_frame_test.index==location, 
                     data_frame_test.columns.astype('int').searchsorted(weight)-1]

location
1    342
Name: 200, dtype: int64

您可以创建一个自定义函数,该函数将遍历列以检查正确的列,然后返回该位置的单元格:

def get_val(location, weight, df):
    col = df.columns[0]
    for c in df.columns:
        if weight >= int(c):
            col = c
        else:
            break
    return df.loc[location, col]
get_val(1, 500, data_frame_test)

您可以创建一个自定义函数,该函数将遍历列以检查正确的列,然后返回该位置的单元格:

def get_val(location, weight, df):
    col = df.columns[0]
    for c in df.columns:
        if weight >= int(c):
            col = c
        else:
            break
    return df.loc[location, col]
get_val(1, 500, data_frame_test)

谢谢大家回答这篇帖子。我已经尝试了所有的答案,它们都有效。根据我正在尝试的,Mohit answers做的最简单。谢谢大家回答这篇帖子。我已经尝试了所有的答案,它们都有效。根据我正在尝试的,Mohit answers做的最简单。