> ## 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 Find All Rows With Newline Inside a Column or DataFrame in Pandas
- URL: https://datascientyst.com/find-all-rows-with-newline-inside-column-dataframe-in-pandas/
- Published: 2021-11-04T09:08:30.000Z
- Updated: 2021-11-04T09:08:30.000Z
- Author: John D K
- Tags: Find

Here is the way **to search for newlines in a column or DataFrame in Pandas**:

**(1) Find for newlines in a single column**

```python
df[df['name'].str.contains('\n',regex=False)]

```

**(2) Search for newlines in the whole DataFrame**

```python
df[df.apply(lambda row: row.astype(str).str.contains('\n').any(), axis=1)]

```

Let say that we have the next DataFrame:

| id | name                   | url                       |
| -- | ---------------------- | ------------------------- |
| 1  | Softhints\\nLinux      | https://www.softhints.com |
| 2  | dataplotplus           | https://dataplotplus.com/ |
| 3  | DataScientyst\\nPandas | https://datascientyst.com |
| 4  | test                   | test\\ntest               |

## Search for newlines in single column

If you need to return only the rows which contains newlines - `\n` in a single column - `name`. Then we can use string method - `contains` of Pandas in combination with `regex=False`:

```python
df[df['name'].str.contains('\n',regex=False)]

```

the returned rows are:

| id | name                   | url                       |
| -- | ---------------------- | ------------------------- |
| 1  | Softhints\\nLinux      | https://www.softhints.com |
| 3  | DataScientyst\\nPandas | https://datascientyst.com |

## Search for newlines in multiple columns of DataFrame

If you need to **search for newlines and other break symbols in several columns** \- you can use lambda.

First we will convert the values to string - in order to avoid errors for non string values. Then we are going to search for `\n`:

```python
df[df[['name', 'url']].apply(lambda row: row.astype(str).str.contains('\n').any(), axis=1)]

```

rows which has newlines in those two columns:

| id | name                   | url                       |
| -- | ---------------------- | ------------------------- |
| 1  | Softhints\\nLinux      | https://www.softhints.com |
| 3  | DataScientyst\\nPandas | https://datascientyst.com |
| 4  | test                   | test\\ntest               |

## Search for newlines in multiple columns of DataFrame

**Searching the whole Pandas DataFrame for breaks and newlines again uses lambda**:

```python
df[df.apply(lambda row: row.astype(str).str.contains('\n').any(), axis=1)]

```

result:

| id | name                   | url                       |
| -- | ---------------------- | ------------------------- |
| 1  | Softhints\\nLinux      | https://www.softhints.com |
| 3  | DataScientyst\\nPandas | https://datascientyst.com |
| 4  | test                   | test\\ntest               |

![](https://datascientyst.com/content/images/2021/11/find-all-rows-with-newline-inside-column-dataframe-in-pandas.png)

## Resources

- [Notebook](https://github.com/softhints/Pandas-Tutorials/commit/2fedaa2f78f63bd0e59b337269d546c5253a5bbe?ref=datascientyst.com)
- [pandas.Series.str.contains](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.contains.html?highlight=contains&ref=datascientyst.com#pandas.Series.str.contains)
- [pandas.DataFrame.apply](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html?highlight=apply&ref=datascientyst.com#pandas.DataFrame.apply)