I have following column names in a list:
vars = ['age','balance','day','duration','campaign','pdays','previous','job_admin.','job_blue-collar']
I have one array which consists of array indexes
(array([1, 5, 7], dtype=int64),)
I want to subset the list based on array indexes
Desired output should be
vars = ['balance','pdays','job_admin.']
I have tried something like this in python
for i, a in enumerate(X):
if i in new_L:
print i
But,it does not work.
Simply use a loop to do that:
result=[]
for i in your_array:
result.append(vars[i])
or one linear
[vars[i] for i in your_array]
If you're using numpy
anyway, use its advanced indexing
import numpy as np
vars = ['age','balance','day','duration','campaign','pdays',
'previous','job_admin.','job_blue-collar']
indices = (np.array([1, 5, 7]),)
sub_array = np.asarray(vars)[indices]
# --> array(['balance', 'pdays', 'job_admin.'], dtype='<U15')
or if you want a list
sub_list = np.asarray(vars)[indices].tolist()
# --> ['balance', 'pdays', 'job_admin.']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With