How to Find All Rows With Newline Inside a Column or DataFrame in Pandas

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

(1) Find for newlines in a single column

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

(2) Search for newlines in the whole DataFrame

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:

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:

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:

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

Resources