处理Dataframe列中的列表

时间:2016-09-16 04:47:32

标签: python pandas numpy dataframe

我使用neighbours创建了一个DataFrame sim_measure_i,它也是一个DataFrame。

neighbours= sim_measure_i.apply(lambda s: s.nlargest(k).index.tolist(), axis =1)

neighbours看起来像这样:

1500                       [0, 1, 2, 3, 4]
1501                       [0, 1, 2, 3, 4]
1502                       [0, 1, 2, 3, 4]
1503     [7230, 12951, 13783, 8000, 18077]
1504                     [1, 3, 6, 27, 47]

这里的第二列有列表 - 我想迭代这个DataFrame并在列表上工作,这样我就可以读取列表中的每个元素 - 例如7230并在另一个DataFrameI中查找7230的得分包含(id,得分了)。

然后,我想在此DataFrame中添加一个列,使其看起来像

test_case_id               nbr_list             scores             
1500                       [0, 1, 2, 3, 4]        [+1, -1, -1, +1, -1]
1501                       [0, 1, 2, 3, 4]        [+1, +1, +1, -1, -1]
1502                       [0, 1, 2, 3, 4]        [+1, +1, +1, -1, -1]
1503     [7230, 12951, 13783, 8000, 18077]        [+1, +1, +1, -1, -1]
1504                     [1, 3, 6, 27, 47]        [+1, +1, +1, -1, -1]

编辑:我写了一个方法get_scores()

def get_scores(list_of_neighbours):
    score_matrix = []
    for x, val in enumerate(list_of_neighbours):
        score_matrix.append(df.iloc[val].score)
    return score_matrix

当我尝试在每个lambda上使用nbr_list时,我收到此错误:

TypeError: ("cannot do positional indexing on <class 'pandas.indexes.numeric.Int64Index'> with these indexers [0] of <type 'str'>", u'occurred at index 1500')

导致此问题的代码:

def nearest_neighbours(similarity_matrix, k):
    neighbours = pd.DataFrame(similarity_matrix.apply(lambda s: s.nlargest(k).index.tolist(), axis =1))
    neighbours = neighbours.rename(columns={0 : 'nbr_list'})

    nbr_scores = neighbours.apply(lambda l: get_scores(l.nbr_list), axis=1)

    print neighbours

3 个答案:

答案 0 :(得分:1)

您可以尝试嵌套循环:

for i in range(neighbours.shape[0]): #iterate over each row
    for j in range(len(neighbours['neighbours_lists'].iloc[i])): #iterate over each element of the list
        a = neighbours['neighbours_lists'].iloc[i][j] #access the element of the list index j in cell location of row i 

其中i是外循环变量,它遍历每一行,j是内循环变量,它遍历每个单元格内列表的长度。

答案 1 :(得分:1)

原始数据框:

In [68]: df
Out[68]: 
   test_case_id                   neighbours_lists
0          1500                    [0, 1, 2, 3, 4]
1          1501                    [0, 1, 2, 3, 4]
2          1502                    [0, 1, 2, 3, 4]
3          1503  [7230, 12951, 13783, 8000, 18077]
4          1504                  [1, 3, 6, 27, 47]

自定义函数,它接受id和list并进行一些计算以评估得分:

In [69]: def g(_id, nbs):
    ...:     return ['-1' if (_id + 1) % (nb + 1) else '+1' for nb in nbs]
    ...: 

Apply自定义函数到原始数据框的所有行:

In [70]: scores = df.apply(lambda x: g(x.test_case_id, x.neighbours_lists), axis=1)

Convert数据框的分数系列和原始数据框的concat

In [71]: df = pd.concat([df, scores.to_frame(name='scores')], 1)

In [72]: df
Out[72]: 
   test_case_id                   neighbours_lists                scores
0          1500                    [0, 1, 2, 3, 4]  [+1, -1, -1, -1, -1]
1          1501                    [0, 1, 2, 3, 4]  [+1, +1, -1, -1, -1]
2          1502                    [0, 1, 2, 3, 4]  [+1, -1, +1, -1, -1]
3          1503  [7230, 12951, 13783, 8000, 18077]  [-1, -1, -1, -1, -1]
4          1504                  [1, 3, 6, 27, 47]  [-1, -1, +1, -1, -1]

答案 2 :(得分:1)

假设您从neighbors开始,就像这样。

In [87]: neighbors = pd.DataFrame({'neighbors_list': [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]})

In [88]: neighbors
Out[88]: 
    neighbors_list
0  [0, 1, 2, 3, 4]
1  [0, 1, 2, 3, 4]

您没有准确指定其他DataFrame(包含id-score对看起来如何),所以这里是近似值。

In [89]: id_score = pd.DataFrame({'id': [0, 1, 2, 3, 4], 'score': [1, -1, -1, 1, -1]})

In [90]: id_score
Out[90]: 
   id  score
0   0      1
1   1     -1
2   2     -1
3   3      1
4   4     -1

您可以将其转换为字典:

In [91]: d = id_score.set_index('id')['score'].to_dict()

然后apply

In [92]: neighbors.neighbors_list.apply(lambda l: [d[e] for e in l])
Out[92]: 
0    [1, -1, -1, 1, -1]
1    [1, -1, -1, 1, -1]
Name: neighbors_list, dtype: object