[PYTHON] How to substitute a numerical value for a partial match (Note 1)

Usage data

#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.

Actions

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.

Bonus: Use .str.contains ()

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

How to substitute a numerical value for a partial match (Note 1)
Python Note: When assigning a value to a string
How to write a ShellScript Bash for statement
How to create a shortcut command for LINUX
[Note] How to create a Ruby development environment
[Note] How to create a Mac development environment
[Go] How to create a custom error for Sentry
How to create a local repository for Linux OS
How to build a development environment for TensorFlow (1.0.0) (Mac)
How to manage a README for both github and PyPI
How to call a function
How to define multiple variables in a python for statement
How to make a Python package (written for an intern)
[Note] How to use virtualenv
How to write a test for processing that uses BigQuery
How to hack a terminal
How to check if a value exists in an enum
How to get the last (last) value in a list in Python
How to create a SAS token for Azure IoT Hub
How to build a LAMP environment using Vagrant and VirtulBox Note
How to create a label (mask) for segmentation with labelme (semantic segmentation mask)
How to find the memory address of a Pandas dataframe value
[For beginners] How to register a library created in Python in PyPI
Spigot (Paper) Introduction to how to make a plug-in for 2020 # 01 (Environment construction)
How to make a unit test Part.1 Design pattern for introduction
[Python] How to get a value with a key other than value with Enum
How to write a Python class
[For non-programmers] How to walk Kaggle
How to make a slack bot
How to create a Conda package
How to make a crawler --Advanced
How to make a recursive function
How to create a virtual bridge
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
A note for writing Python-like code
How to delete a Docker container
How to make a crawler --Basic
How to create a config file
[Python] What is a formal argument? How to set the initial value
How to make a model for object detection using YOLO in 3 hours
How to get a value from a parameter store in lambda (using python)
A note when looking for an alternative to pandas rolling for moving windows
How to get a sample report from a hash value using VirusTotal's API
How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity
How to deal with scikit-learn's "ValueError: Input contains NaN, infinity or a value too large for dtype ('float64')."
How to create a clone from Github
How to split and save a DataFrame
How to build a sphinx translation environment
How to create a git clone folder
Qiita (1) How to write a code name
How to add a package with PyCharm
[Python] How to make a class iterable
How to draw a graph using Matplotlib
How to create * .spec files for pyinstaller.
[Python] How to convert a 2D list to a 1D list
[Colab] How to copy a huge dataset
[Python] How to invert a character string
How to install a package using a repository
How to get a stacktrace in python