如何从包含数字列表的熊猫细胞中检索特定元素

时间:2019-02-02 11:36:09

标签: python pandas

假设我有一个这样的数据框:

df
       cars                       places
0   [10, 20, 30, 40]            [0, 0, 0, 3]
1   [10, 20, 30, 40, 50, 60]    [0, 0, 1, 2, 3, 5]
2   [10, 20]                    [0, 1]

我想创建第三列,该列仅包含"cars"列中与0列中的数字places对应的那些元素。 因此,所需的输出是

          cars                    places            stolen
0   [10, 20, 30, 40]             [0, 0, 0, 3]       [10, 20, 30]
1   [10, 20, 30, 40, 50, 60]     [0, 0, 1, 2, 3, 5] [10, 20]
2   [10, 20]                     [0, 1]             [10]

最流畅的执行方式是什么?

3 个答案:

答案 0 :(得分:2)

您可以使用Apply功能

df['stolen'] = df.apply(lambda x: [x['cars'][i] for i, j in enumerate(x['places']) if j==0], axis=1)

输出

                       cars              places        stolen
0          [10, 20, 30, 40]        [0, 0, 0, 3]  [10, 20, 30]
1  [10, 20, 30, 40, 50, 60]  [0, 0, 1, 2, 3, 5]      [10, 20]
2                  [10, 20]              [0, 1]          [10]

答案 1 :(得分:1)

您可以这样做:

df['stolen'] = [[car for car, place in zip(cars, places) if not place] for cars, places in zip(df.cars, df.places)]
print(df)

输出

                       cars              places        stolen
0          [10, 20, 30, 40]        [0, 0, 0, 3]  [10, 20, 30]
1  [10, 20, 30, 40, 50, 60]  [0, 0, 1, 2, 3, 5]      [10, 20]
2                  [10, 20]              [0, 1]          [10]

答案 2 :(得分:0)

df['stolen'] = np.where((df['places']==0),df['cars'],'')