List comprehension 370-2

Screen Link:
Working With Missing Data | Dataquest

I understand the rationale of list comprehension and I understand that df.columns will give the name of columns.

My query is on this part “…if “killed” in col”. The word “killed” is not the name of any column. The word “killed” is used with one other word and an underscore. So the query, is how will the word “killed” match with the name of columns i.e. pedestrian_killed or cyclist killed ?

For questions like this, I find it’s best to just play with some code to see how things work. For example, try this:

'killed' in 'cyclist_killed'

and verify the output. Then “up the complexity” a bit with this:

test_col_names = ['pedestrians_killed', 'location', 'motorist_killed', 'date', 'borough']
for col in test_col_names:
    if 'killed' in col:
        print(col)
1 Like