[英]Extracting element from a list in a pandas column based on the value in another column
我有以下pandas df,我想根据num列上的数字从列表列中提取元素:
list num
[1,2,3,4,5] 3
[7,2,1,3,4] 4
获得:
list num element
[1,2,3,4,5] 3 4
[7,2,1,3,4] 4 4
我努力了:
df['element'] = df['list'].apply(lambda x: x[df['num'].apply(lambda y: y)])
但是我得到了TypeError: list indices must be integers or slices, not Series
。
反正我能做到这一点吗? 谢谢!
对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
或列出对zip
理解:
df['element'] = [x[y] for x, y in zip(df['list'], df['num'])]
如果可能,某些值与列表不匹配,则可以使用:
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)
使用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.