简体   繁体   English

根据另一列中的值从pandas列中的列表中提取元素

[英]Extracting element from a list in a pandas column based on the value in another column

I have a following pandas df and I would like to extract the element from the list column based on whatever number that is on the num column: 我有以下pandas df,我想根据num列上的数字从列表列中提取元素:

list             num
[1,2,3,4,5]       3
[7,2,1,3,4]       4

To obtain: 获得:

list             num    element
[1,2,3,4,5]       3        4
[7,2,1,3,4]       4        4

I have tried: 我努力了:

df['element'] = df['list'].apply(lambda x: x[df['num'].apply(lambda y: y)])

But I got TypeError: list indices must be integers or slices, not Series . 但是我得到了TypeError: list indices must be integers or slices, not Series

Is there anyway I can do this? 反正我能做到这一点吗? Thank you! 谢谢!

Use DataFrame.apply per rows with axis=1 : axis=1每行使用DataFrame.apply

df['element'] = df.apply(lambda x: x['list'][x['num']], axis=1)
print (df)
              list  num  element
0  [1, 2, 3, 4, 5]    3        4
1  [7, 2, 1, 3, 4]    4        4

Or list comprehension with zip : 或列出对zip理解:

df['element'] = [x[y]  for x, y in zip(df['list'], df['num'])]

If possible some values not match of list, here is possible use: 如果可能,某些值与列表不匹配,则可以使用:

def func(a, b):
    try:
        return a[b]
    except Exception:
        return np.nan    

df['element'] = df.apply(lambda x: func(x['list'], x['num']), axis=1)

Using numpy fancy index 使用numpy花式索引

list_val = np.array(df.list.values.tolist())
num_val = df.num.values
df['element'] = list_val[np.arange(df.shape[0]), num_val]

Out[295]:
              list  num  element
0  [1, 2, 3, 4, 5]    3        4
1  [7, 2, 1, 3, 4]    4        4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM