regex
Syntax
Usage
Use re
1 | re.sub(pattern,repl,str) |
Use Pandas
1 | df['column_name'].str.contains('pattern') |
Example
Example 1
Extracting room numbers from a ‘Description’ column in a DataFrame using regular expressions:
Import the
re
module:1
2
3
4
5
6
7
8
9
10
11
12import re
````
2. Define a function to extract the room number from a description:
```python
def extract_room_number(description):
match = re.search(r'(\d+\.\d+|\d+)(?=\s+of which are bedrooms)', description)
if match:
return float(match.group(1))
else:
return NoneUse the
apply()
method to apply the function to the ‘Description’ column and create a new ‘RoomNumber’ column:
1 | df['RoomNumber'] = df['Description'].apply(extract_room_number) |
This will extract the room number from the ‘Description’ column and store it in the new ‘RoomNumber’ column.