#Usage data
df
name | room_type | |
---|---|---|
0 | Hotel A | Superior Twin Room Non-Smoking |
1 | Hotel B | Superior Twin Room Smoking |
2 | Hotel C | Deluxe twin room |
3 | Hotel D | Standard twin room non-smoking |
4 | Hotel E | A double room |
5 | Hotel F | Deluxe Double Room Non-Smoking |
6 | Hotel G | Standard twin room non-smoking |
7 | Hotel H | Deluxe King Room |
I prepared something like this.
Goal
Create a new column with room_type plus 1 for those named'Superior'and'Deluxe', and 0 for those that don't.
new_room_type_list = []
for i in range(8):
if 'Superior' in df['room_type'][i]:
a = 1
elif 'Deluxe' in df['room_type'][i]:
a = 1
else:
a = 0
new_room_type_list.append(a)
Create a list like this Then add it to df.
#new created in df_room_type_list df['new_room_type']Add as.
df['new_room_type'] = new_room_type_list
I will check
df
name | room_type | new_room_type | |
---|---|---|---|
0 | Hotel A | Superior Twin Room Non-Smoking | 1 |
1 | Hotel B | Superior Twin Room Smoking | 1 |
2 | Hotel C | Deluxe twin room | 1 |
3 | Hotel D | Standard twin room non-smoking | 0 |
4 | Hotel E | A double room | 0 |
5 | Hotel F | Deluxe Double Room Non-Smoking | 1 |
6 | Hotel G | Standard twin room non-smoking | 0 |
7 | Hotel H | Deluxe King Room | 1 |
I was able to add it like this.
df['room_type'].str.contains('Superior')
0 True 1 True 2 False 3 False 4 False 5 False 6 False 7 False Name: room_type, dtype: bool
In this way, you can see in which line'superior'appears, but since .str.contains () covers the entire DataFrame, it is judged that it is not suitable for if statements that look at each line, so the above Using the method
Recommended Posts