Consider the df
:
考虑一下df:
Index 0_usg 1_usg 2_usg 0_pre 1_pre 2_pre 3_pre
0 1.2 1.1 0.99 2.1 2.5 1.9 1.8
1 1.0 1.1 0.99 2.2 2.5 1.9 2.1
2 0.9 1.4 0.80 1.9 2.5 1.9 2.0
3 1.3 2.1 1.21 2.6 1.9 1.8 2.3
4 1.5 1.0 0.99 0.9 2.0 1.9 2.0
I want to identify the column (among the columns related to usg
) where the value is closest to 1 and column (among those related to pre
) where value is closest to 2.
我想确定值最接近1的列(在与usg相关的列中)和列(在与pre相关的列中),其中值最接近2。
The expected output is:
预期的输出是:
Index usg pre
0 2_usg 0_pre
1 0_usg 3_pre
2 0_usg 3_pre
3 2_usg 1_pre
4 1_usg 1_pre
Note that if two items in two columns are equidistant, you can choose either. How do I get this efficiently?
请注意,如果两列中的两个项目是等距的,则可以选择其中一个。我如何有效地获得这个?
2 个解决方案
#1
4
You can use first filter
, then substract 1
and 2
, get abs
with idxmin
:
您可以使用第一个过滤器,然后减去1和2,使用idxmin获取abs:
usg = df.filter(like='usg').sub(1).abs().idxmin(axis=1)
pre = df.filter(like='pre').sub(2).abs().idxmin(axis=1)
df1 = pd.DataFrame({'usg':usg,'pre':pre}, index=df.index, columns=['usg','pre'])
print (df1)
usg pre
0 2_usg 0_pre
1 0_usg 2_pre
2 0_usg 3_pre
3 2_usg 1_pre
4 1_usg 1_pre
#2
3
You could do
你可以做到
In [37]: pd.concat([
(df.filter(regex='pre') - 2).abs().idxmin(axis=1),
(df.filter(regex='usg') - 1).abs().idxmin(axis=1)
], axis=1)
Out[37]:
0 1
0 0_pre 2_usg
1 2_pre 0_usg
2 3_pre 0_usg
3 1_pre 2_usg
4 1_pre 1_usg
#1
4
You can use first filter
, then substract 1
and 2
, get abs
with idxmin
:
您可以使用第一个过滤器,然后减去1和2,使用idxmin获取abs:
usg = df.filter(like='usg').sub(1).abs().idxmin(axis=1)
pre = df.filter(like='pre').sub(2).abs().idxmin(axis=1)
df1 = pd.DataFrame({'usg':usg,'pre':pre}, index=df.index, columns=['usg','pre'])
print (df1)
usg pre
0 2_usg 0_pre
1 0_usg 2_pre
2 0_usg 3_pre
3 2_usg 1_pre
4 1_usg 1_pre
#2
3
You could do
你可以做到
In [37]: pd.concat([
(df.filter(regex='pre') - 2).abs().idxmin(axis=1),
(df.filter(regex='usg') - 1).abs().idxmin(axis=1)
], axis=1)
Out[37]:
0 1
0 0_pre 2_usg
1 2_pre 0_usg
2 3_pre 0_usg
3 1_pre 2_usg
4 1_pre 1_usg