如何从最近的列中获取值?
how to get value from the nearest column?
训练数据集
df_table = pd.DataFrame()
df_table["A"] = [0.2, 0.2, 0.2, 0.2, 0.2,
0.4, 0.4, 0.4, 0.4, 0.4,
0.6, 0.6, 0.6, 0.6, 0.6,
1.5, 1.5, 1.5, 1.5, 1.5,
2.5, 2.5, 2.5, 2.5, 2.5,
3.0, 3.0, 3.0, 3.0, 3.0]
df_table[450] = [1, 5, 20, 30, 40,
1, 5, 8, 10, 20,
1, 5, 10, 15, 25,
2, 7, 15, 20, 30,
2, 7, 15, 20, 35,
2, 8, 20, 30, 40]
df_table[500] = [3, 15, 25, 60, 80,
4, 10, 15, 20, 30,
5, 10, 15, 30, 40,
5, 10, 20, 30, 45,
7, 10, 20, 35, 50,
8, 15, 25, 60, 80]
如果的输入值为 A = 0.
2,并且 temperature = 476
,我想从最接近 476
的列中获取值,在这种情况下,值来自 500
将是 3
.
同样,如何从 450
和 500
中获取两个值。对于此实例 1
和 3
。所以这将是最近的较小列和最近的较高列的值。
您需要根据可用列和您的值计算要使用的正确列。
然后本地化数据框的第一个值,其中 A
填充您搜索的值并查询您刚刚计算的列:
import pandas as pd
df_table = pd.DataFrame()
df_table["A"] = [0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.4,
0.6, 0.6, 0.6, 0.6, 0.6, 1.5, 1.5, 1.5, 1.5, 1.5,
2.5, 2.5, 2.5, 2.5, 2.5, 3.0, 3.0, 3.0, 3.0, 3.0]
df_table[450] = [1, 5, 20, 30, 40, 1, 5, 8, 10, 20,
1, 5, 10, 15, 25, 2, 7, 15, 20, 30,
2, 7, 15, 20, 35, 2, 8, 20, 30, 40]
df_table[500] = [3, 15, 25, 60, 80, 4, 10, 15, 20, 30,
5, 10, 15, 30, 40, 5, 10, 20, 30, 45,
7, 10, 20, 35, 50, 8, 15, 25, 60, 80]
# integer columns harvested from the dataframe
T_values = [n for n in df_table.columns if isinstance(n,int)]
# what I want to query
myA = 0.2
myT = 476
# get the column names that are near myT
minColNear_myT = max(a for a in T_values if a <= myT)
maxColNear_myT = min(a for a in T_values if a >= myT)
# localize the first one where myA matches
first_one_matching_myA = df_table.loc[(df_table['A'] == myA).idxmax()]
# output the values of the columns near myT
print(minColNear_myT, myT, first_one_matching_myA[minColNear_myT])
print(maxColNear_myT, myT, first_one_matching_myA[maxColNear_myT])
输出:
450 476 1.0
500 476 3.0
要仅输出较近的列,将 myT
与存在的两列进行比较,并使用差异最小的列:
closest = sorted( (k for k in T_values), key = lambda x:abs(x - myT) )[0]
print(closest, myT, first_one_matching_myA[closest])
训练数据集
df_table = pd.DataFrame()
df_table["A"] = [0.2, 0.2, 0.2, 0.2, 0.2,
0.4, 0.4, 0.4, 0.4, 0.4,
0.6, 0.6, 0.6, 0.6, 0.6,
1.5, 1.5, 1.5, 1.5, 1.5,
2.5, 2.5, 2.5, 2.5, 2.5,
3.0, 3.0, 3.0, 3.0, 3.0]
df_table[450] = [1, 5, 20, 30, 40,
1, 5, 8, 10, 20,
1, 5, 10, 15, 25,
2, 7, 15, 20, 30,
2, 7, 15, 20, 35,
2, 8, 20, 30, 40]
df_table[500] = [3, 15, 25, 60, 80,
4, 10, 15, 20, 30,
5, 10, 15, 30, 40,
5, 10, 20, 30, 45,
7, 10, 20, 35, 50,
8, 15, 25, 60, 80]
如果的输入值为 A = 0.
2,并且 temperature = 476
,我想从最接近 476
的列中获取值,在这种情况下,值来自 500
将是 3
.
同样,如何从 450
和 500
中获取两个值。对于此实例 1
和 3
。所以这将是最近的较小列和最近的较高列的值。
您需要根据可用列和您的值计算要使用的正确列。
然后本地化数据框的第一个值,其中 A
填充您搜索的值并查询您刚刚计算的列:
import pandas as pd
df_table = pd.DataFrame()
df_table["A"] = [0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.4,
0.6, 0.6, 0.6, 0.6, 0.6, 1.5, 1.5, 1.5, 1.5, 1.5,
2.5, 2.5, 2.5, 2.5, 2.5, 3.0, 3.0, 3.0, 3.0, 3.0]
df_table[450] = [1, 5, 20, 30, 40, 1, 5, 8, 10, 20,
1, 5, 10, 15, 25, 2, 7, 15, 20, 30,
2, 7, 15, 20, 35, 2, 8, 20, 30, 40]
df_table[500] = [3, 15, 25, 60, 80, 4, 10, 15, 20, 30,
5, 10, 15, 30, 40, 5, 10, 20, 30, 45,
7, 10, 20, 35, 50, 8, 15, 25, 60, 80]
# integer columns harvested from the dataframe
T_values = [n for n in df_table.columns if isinstance(n,int)]
# what I want to query
myA = 0.2
myT = 476
# get the column names that are near myT
minColNear_myT = max(a for a in T_values if a <= myT)
maxColNear_myT = min(a for a in T_values if a >= myT)
# localize the first one where myA matches
first_one_matching_myA = df_table.loc[(df_table['A'] == myA).idxmax()]
# output the values of the columns near myT
print(minColNear_myT, myT, first_one_matching_myA[minColNear_myT])
print(maxColNear_myT, myT, first_one_matching_myA[maxColNear_myT])
输出:
450 476 1.0
500 476 3.0
要仅输出较近的列,将 myT
与存在的两列进行比较,并使用差异最小的列:
closest = sorted( (k for k in T_values), key = lambda x:abs(x - myT) )[0]
print(closest, myT, first_one_matching_myA[closest])