> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Extract Everything Before or After with Regex in Pandas
- URL: https://datascientyst.com/extract-everything-before-after-regex-pandas/
- Published: 2023-09-04T07:16:10.000Z
- Updated: 2023-09-04T07:16:10.000Z
- Author: John D K
- Tags: Regex

To plot two variables on two sides of Y-axes, we can plot in two steps:

- `'(.*?)\n'`
- `'.+?(?=\n)'`

## Steps to extract everything until/after

Below are the steps which I usually follow for regex extraction in Pandas

- analyse the data from which I will extract
- clean the data
- choose pandas method - `split`, `extract` etc
- define regex pattern
- create new column(s)

## Data

Let's create simple sample DataFrame to be used for regex extraction:

```python
from faker import Faker
import pandas as pd

Faker.seed(0)
fake = Faker()
addr = []
for _ in range(5):
    addr.append(fake.address())
df = pd.DataFrame({'address':addr})

```

|   | address                                              |
| - | ---------------------------------------------------- |
| 0 | 48764 Howard Forge Apt. 421\\nVanessaside, VT 79393  |
| 1 | PSC 4115, Box 7815\\nAPO AA 41945                    |
| 2 | 778 Brown Plaza\\nNorth Jenniferfurt, VT 88077       |
| 3 | 3513 John Divide Suite 115\\nRodriguezside, LA 93111 |
| 4 | 398 Wallace Ranch Suite 593\\nIvanburgh, AZ 80818    |

## Example 1 - Captcharing group and characters

Extract everything in Pandas column up to new line

```python
df['address'].str.extract('(.*?)\n')

```

result:

|   | 0                           |
| - | --------------------------- |
| 0 | 48764 Howard Forge Apt. 421 |
| 1 | PSC 4115, Box 7815          |
| 2 | 778 Brown Plaza             |
| 3 | 3513 John Divide Suite 115  |
| 4 | 398 Wallace Ranch Suite 593 |

## Example 2 - Non captcharing groups

Extract everything in Pandas column up to new line

```python
df['address'].str.extract('(.+)?(?=\n)')

```

result:

|   | 0                           |
| - | --------------------------- |
| 0 | 48764 Howard Forge Apt. 421 |
| 1 | PSC 4115, Box 7815          |
| 2 | 778 Brown Plaza             |
| 3 | 3513 John Divide Suite 115  |
| 4 | 398 Wallace Ranch Suite 593 |

## Output

![](https://datascientyst.com/content/images/2023/09/extract-everything-before-after-regex-pandas.png)

## Resources

- [Working with text data](https://pandas.pydata.org/pandas-docs/stable/user%5Fguide/text.html?ref=datascientyst.com)
- [pandas.Series.str.extract](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.extract.html?ref=datascientyst.com)
- [pandas.Series.str.contains](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.contains.html?ref=datascientyst.com)