从pandas数据框中选择行,在多列上使用numpy 2D数组

nbe*_*hat 6 python select numpy dataframe pandas

数据

我有一个包含5列的数据框:

  • 纬度和经度(origin_lat,origin_lng)
  • 目的地的纬度和经度(dest_lat,dest_lng)
  • 从其他字段计算的分数

我有一个M包含原点和目的地纬度/经度对的矩阵.其中一些对存在于数据帧中,另一些则不存在.

目标

我的目标是双重的:

  1. 选择数据框M的前四列中不存在的所有对,func向它们应用函数(计算得分列),并将结果附加到现有数据框.注意:我们不应重新计算现有行的分数.
  2. 添加缺失的行后,M在新数据框中选择由选择矩阵定义的所有行dfs.

示例代码

# STEP 1: Generate example data
ctr_lat = 40.676762
ctr_lng = -73.926420
N = 12
N2 = 3

data = np.array([ctr_lat+np.random.random((N))/10,
                 ctr_lng+np.random.random((N))/10,
                 ctr_lat+np.random.random((N))/10,
                 ctr_lng+np.random.random((N))/10]).transpose()

# Example function - does not matter what it does
def func(x):
    return np.random.random()

# Create dataframe
geocols = ['origin_lat','origin_lng','dest_lat','dest_lng']
df = pd.DataFrame(data,columns=geocols)
df['score'] = df.apply(func,axis=1)
Run Code Online (Sandbox Code Playgroud)

这给了我一个df这样的数据帧:

    origin_lat  origin_lng   dest_lat   dest_lng     score
0    40.684887  -73.924921  40.758641 -73.847438  0.820080
1    40.703129  -73.885330  40.774341 -73.881671  0.104320
2    40.761998  -73.898955  40.767681 -73.865001  0.564296
3    40.736863  -73.859832  40.681693 -73.907879  0.605974
4    40.761298  -73.853480  40.696195 -73.846205  0.779520
5    40.712225  -73.892623  40.722372 -73.868877  0.628447
6    40.683086  -73.846077  40.730014 -73.900831  0.320041
7    40.726003  -73.909059  40.760083 -73.829180  0.903317
8    40.748258  -73.839682  40.713100 -73.834253  0.457138
9    40.761590  -73.923624  40.746552 -73.870352  0.867617
10   40.748064  -73.913599  40.746997 -73.894851  0.836674
11   40.771164  -73.855319  40.703426 -73.829990  0.010908
Run Code Online (Sandbox Code Playgroud)

然后,我可以人工创建选择矩阵M,其中包含3个存在于数据帧中的行,3个行则不存在.

# STEP 2: Generate data to select
# As an example, I select 3 rows that are part of the dataframe, and 3 that are not
data2 = np.array([ctr_lat+np.random.random((N2))/10,
                  ctr_lng+np.random.random((N2))/10,
                  ctr_lat+np.random.random((N2))/10,
                  ctr_lng+np.random.random((N2))/10]).transpose()

M = np.concatenate((data[4:7,:],data2))
Run Code Online (Sandbox Code Playgroud)

矩阵M看起来像这样:

array([[ 40.7612977 , -73.85348031,  40.69619549, -73.84620489],
       [ 40.71222463, -73.8926234 ,  40.72237185, -73.86887696],
       [ 40.68308567, -73.84607722,  40.73001434, -73.90083107],
       [ 40.7588412 , -73.87128079,  40.76750639, -73.91945371],
       [ 40.74686156, -73.84804047,  40.72378653, -73.92207075],
       [ 40.6922673 , -73.88275402,  40.69708748, -73.87905543]])
Run Code Online (Sandbox Code Playgroud)

从这里,我不知道如何知道哪些行M不存在df并添加它们.我也不知道如何选择全部来自行df认为是M.

思路

我的想法是识别丢失的行,dfnan分数附加它们,并仅重新计算nan行的分数.但是,我不知道如何在没有循环矩阵的每个元素的情况下有效地选择这些行M.

有什么建议吗?非常感谢你的帮助!

igr*_*nis 7

有什么理由不使用merge

df2 = pd.DataFrame(M, columns=geocols) 
df = df.merge(df2, how='outer')
ix = df.score.isnull()
df.loc[ix, 'score'] = df.loc[ix].apply(func, axis=1)
Run Code Online (Sandbox Code Playgroud)

它完全符合您的建议:添加缺少的行df和一个纳分,识别nans,计算这些行的分数.