Python np.where与列表元素选择

Python np.where与列表元素选择,python,list,numpy,Python,List,Numpy,我有一个如下所示的数据示例(真实数据集有更多列): 我只想在IDct number大于1时(因为IDct number表示列表中有多少个元素)获取Index1和Index2列(它们是列表)中的第二个元素 我试图回答以下问题,但都出现了“列表索引超出范围”的错误 data['pos'] = np.where(data['IDct']>1, data.Index1.map(lambda x: x[1]),0) data['pos1']= np.where(data['IDct']>1,

我有一个如下所示的数据示例(真实数据集有更多列):

我只想在IDct number大于1时(因为IDct number表示列表中有多少个元素)获取Index1和Index2列(它们是列表)中的第二个元素

我试图回答以下问题,但都出现了“列表索引超出范围”的错误

data['pos'] = np.where(data['IDct']>1, data.Index1.map(lambda x: x[1]),0)
data['pos1']= np.where(data['IDct']>1, data.Index2.map(lambda x: x[1]),0)


我应该怎么做?谢谢

您可以尝试使用loc分配列

data.loc[data['IDct'] > 1, 'pos'] = data.loc[data['IDct'] > 1]['Index1'].apply(lambda x: x[1])
data.loc[data['IDct'] > 1, 'pos1'] = data.loc[data['IDct'] > 1]['Index2'].apply(lambda x: x[1])

除非绝对必要,否则请不要以图像形式共享信息。见:。
data['pos'] = np.where(data['IDct']>1, [x[1] for x in data['Index1']],0)
data['pos1']= np.where(data['IDct']>1, [x[1] for x in data['Index2']],0)
data.loc[data['IDct'] > 1, 'pos'] = data.loc[data['IDct'] > 1]['Index1'].apply(lambda x: x[1])
data.loc[data['IDct'] > 1, 'pos1'] = data.loc[data['IDct'] > 1]['Index2'].apply(lambda x: x[1])