this code should generate a list called 'cat_list' of values taken from df['a'] according to their position in the 'cat' list. If df['a'] contains values not present in 'cat' list, then 0 should be appended to 'cat_list'. 'cat_list' should have length 6, but I am not sure why its length is 18.
import pandas as pd
d = {'a': [0.1, 0.2,0.3,0.4,0.5,0.6], 'b': [0.6, 0.8,0.3,0.4,0.1,0.1],
'c': [0.7, 0.3,0.9,0.4,1.0,0.2],'d': [1,0,0,1,0,1]}
df = pd.DataFrame(data=d)
cat=[0.6,0.3,0.1]
cat_list=[]
for i in df.a:
for j in cat:
if i == j:
cat_list.append(cat.index(j))
else:
cat_list.append(0)
print(cat_list) # should print [2,0,1,0,0,0]
print(len(cat_list)) # should print 6, not 18