首页 > 解决方案 > 获取选定值的索引

问题描述

我有三个数据框。一:我要补充数据的房屋基本地理信息。二:从房屋(行)到四个来源(列)之一的距离。三:房子到源头的对应角度(0-360度)。

一:

           X          Y    ...      Area_Cat        UID
0                          ...                         
1  142862.10  391169.10    ...             1  67321NY15 
2  143687.10  391063.10    ...             1   67321NY4 
3  144728.45  390877.88    ...             1   67321NY6 
4  144842.32  391811.89    ...             1   67321NY7
5  145386.77  392740.08    ...             1 67321NY147  

[5 rows x 11 columns]

二:

         1        2        3        4
1  1807.04  1894.98  2135.75  2396.95
2  1801.63  1594.55  1606.38  1744.48
3  2323.27  1835.68  1485.06  1317.95
4   1692.7  1084.16  586.009  400.732
5  1880.35  1293.06  842.389  675.357

三:

         1        2        3        4
1  201.011  220.827   236.11   245.66
2  174.359  195.045  216.163  231.166
3  148.368  160.013  176.392  193.942
4  128.085  136.861  159.281  210.549
5  93.5344  83.9145  63.1797  30.3033

我已经成功地使用以下方法将最短距离添加到数据框:

    for index, row in two.iterrows():
         one.loc[index,'Distance'] = min(row)

结果:

           X          Y     ...              UID     Distance
0                           ...                              
1  142862.10  391169.10     ...       67321NY15   1807.043447
2  143687.10  391063.10     ...        67321NY4   1594.554866
3  144728.45  390877.88     ...        67321NY6   1317.947638
4  144842.32  391811.89     ...        67321NY7    400.732398
5  145386.77  392740.08     ...      67321NY147    675.356557

[5 rows x 12 columns]

现在我想用 column name 添加相应的角度Orientation。我的想法是找到该min(row)值的列和行索引,并使用它们来用第三个数据框中的值填充新列。我找到了idxmin()选择器,但几次尝试都失败了。你能帮我吗?

期望的结果:

           X          Y     ...              UID     Distance  Orientation
0                           ...                              
1  142862.10  391169.10     ...       67321NY15   1807.043447      201.011
2  143687.10  391063.10     ...        67321NY4   1594.554866      195.045
3  144728.45  390877.88     ...        67321NY6   1317.947638      193.942
4  144842.32  391811.89     ...        67321NY7    400.732398      210.549
5  145386.77  392740.08     ...      67321NY147    675.356557      30.3033

[5 rows x 12 columns]

标签: pythonpandas

解决方案


我认为使用idxmin并从 Two 获取最小索引是个好主意!
为了使用索引从其他数据帧(例如 Three)获取数据,我使用了pd.DataFrame.valueslist comprehensionzip

# get the indexes of min values from Two
ix = two.idxmin(axis=1)
# result:
#     ix => [ 1, 2, 4, 4, 4 ]

# get the distance and orientation from Two and Three using the above indexes
_two = [two_val[i] for two_val, i in zip(two.values, ix)]
_three = [three_val[i] for three_val, i in zip(three.values, ix)]
# result:
#     _two => [ 1807.043447, 1594.554866, 1317.947638, 400.732398, 675.356557 ]
#     _three => [ 201.011, 195.045, 193.942, 210.549, 30.3033 ]

# append the result to One (be careful with the 0 index in One)
one["Distance"] = ””
one.loc[1:, ”Distance”] = _two
one["Orientation"] = ””
one.loc[1:, ”Orientation”] = _three

在这里,我对 Two 使用了相同的方法(使用 Two 的 idxmin 输出获取值),但您的原始方法也适用。

编辑:意识到数据框 One 有第 0 个索引,我在那里附加了一个空字符串


推荐阅读