I wish to create a new list which contains column names of those columns which have atleast one of the following values.
- Most of the time
- Quite Often
- Less than often
- Never
Sample :
df ={‘A’:[‘name1’, ‘name2’, ‘name3’, ‘name4’],
JavaScript
x
11
11
1
'B':['Most of the Time', 'Never', 'Quite Often', 'Less than
2
Often'],
3
4
'C':['abc', 'DEF', 'ghi', 'Jkl'],
5
'D':['1', '2', '3', '4'],
6
'E':['Most of the Time', 'Never', 'Quite Often', 'Less than
7
Often'],
8
'F':['Most of the Time', 'Never', 'Quite Often', 'Less than
9
Often']
10
}
11
h_ls = [‘B’,’E’,’F’]
I tried the following code
JavaScript
1
18
18
1
df.columns
2
3
h_ls=[]
4
5
6
7
for x in df.columns:
8
xi = str(x)
9
for i in df[x]:
10
if i.startswith("Most of the Time") or i.startswith("Quite
11
Often") or i.startswith("Less that Often") or
12
i.startswith("Never"):
13
14
h_ls.append(xi)
15
break
16
else:
17
continue
18
I get an error that says ‘Timestamp’ object has no attribute ‘startswith’
It gets stuck on the first column where the condition is false.
Can anyone tell me where I am committing the mistake or if there exists a better solution? I have dropped the timestamp column from the data frame but still it’s popping up.
Advertisement
Answer
Here is another way:
JavaScript
1
2
1
df.columns[df.isin(accepted_strings).any()].tolist()
2
Output:
JavaScript
1
2
1
['B', 'E', 'F']
2