I have a column in a pandas df named specialty that looks like this:
0 1,5
1 1
2 1,2,4,6
3 2
4 1
5 1,5
6 3
7 3
8 1
9 2,3
I'd like to create a new column called is_1 that contains a 1 for all rows in specialty that contain a 1 and a 0 for rows that don't contain a 1. The output would look like this:
0 1
1 1
2 1
3 0
4 1
5 1
6 0
7 0
8 1
9 0
I'm not sure how to do this with a column of mixed dtypes. Would I just use np.where() with a str.contains() call? Like so:
np.where((part_chars['specialty'] == 1) | part_chars['specialty'].str.contains('1'), 1, 0)
Yep that works...
Update Your code works well for me.
If you have mixed dtype, you can force the dtype with
.astype(str):You can use
str.contains:Alternative with
str.split: