I have a data sample look like this (real dataset has more columns):
data = {'stringID':['AB CD Efdadasfd','RFDS EDSfdsadf dsa','FDSADFDSADFFDSA'],'IDct':[1,2,3]}
data = pd.DataFrame(data)
data['Index1'] = [[3],[7,9],[5,6,8]]
data['Index2'] = [[4],[10,13],[8,9,10]]
I want to get the second element in Index1 and Index2 columns (which are list) only if IDct number is bigger than 1(because IDct number indicates how many elements are there in the list).
I was trying following answers but all getting the error of 'list index out of range'
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)
or
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)
What should i do differently? Thanks!
You can try to use loc to assign the columns.
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.