是否有一种简单的方法来以numpy的方式查找最接近的值? 并获得输出行号?
作为一个例子:我想找到最接近的行
value= [0.33, 0.66, 0.99]
我有数据
random= pd.DataFrame()
random=random.assign(data='')
random.data= np.linspace(0.1,1.0,10)
data
0 0.1
1 0.2
2 0.3
3 0.4
4 0.5
5 0.6
6 0.7
7 0.8
8 0.9
9 1.0
作为答案,该函数现在应给出data
最接近value
的行
answer= [2,6,9]
我正在尝试与
idx = (np.abs(random.data - value)).argmin()
但是我正在用循环和行作为答案。
有人知道一种简单的方法吗? :-)
答案 0 :(得分:2)
df = pd.DataFrame({'data':np.linspace(0.1, 1.0, 10)})
values = [0.33, 0.66, 0.99]
d = np.abs(values - df.values)
np.argmin(d, axis=0)
这将返回第一个最接近值的索引。如果数据框中有多个值与values
中的值相等,并且想要返回所有索引,则可以使用:
np.where(d == d.min(0))[0]
代替np.argmin
。
答案 1 :(得分:0)
尝试:
int Idd = Convert.ToInt32(your value for the combobox you want to be selected);
for (int i = 0; i < myComboBox.Items.Count; i++)
{
myComboBox.SelectedIndex = i;
if (Convert.ToInt32( myComboBox.SelectedValue ) == Idd )
{break;}
}
在一个函数中,它将是:
struct MSProduct: Codable {
let categories : [Categories]?
let tags : [Tags]?
let images : [Images]?
let attributes : [Attributes]?
let default_attributes : [String]?
let variations : [String]?
let grouped_products : [String]?
let menu_order : Int?
let meta_data : [MetaData]?
let _links : Links?
}
struct Attributes : Codable {
let id: Int?
let name: String?
let position: Int?
let visible: Bool?
let variation: Bool?
let options: [String]?
}
致谢