I have a problem. I have a list which contains data frame column names it should match with the data frame if the name is matched it should get appended in a new list. I wrote code which does that but the names are getting appended according to the sequence of data frame but not according to list. How do I do that?
Input: col_lst = ['hj','pl','fgc','bc','drt'] df= abc bc fgc drt 0 0 q d 3 1 0 g t 1 2 0 a g 4 3 0 d c 5 4 0 h b 6
My code:
lst=[] for i in df: for j in col_lst: if i in j: lst.append(j)
I get the Ouput like this lst =['bc','fgc','drt']
lst =['bc','fgc','drt']
what I want is lst = ['fgc ','bc','drt']
lst = ['fgc ','bc','drt']
If you can do like this give the name as listwise see following stuff
col_lst = ['hj','pl','fgc','bc','drt'] lst=[] for j in col_lst: if j in df.columns: lst.append(j) print(lst)
output will be
['fgc ','bc','drt']
1.4m articles
1.4m replys
5 comments
57.0k users