Daxesh Radadiya
Daxesh Radadiya

Reputation: 73

How to avoid new line as a delimeter in pandas dataframe

I have an excel sheet which has a column as remarks. So, for example, a cell contains data in a format like

  1. There is a book.
  2. There is also a pen along with the book.
  3. So, I decided to study for a while.

When I convert that excel into a pandas data frame, the data frame only captures the 1st point till the new line. It won't capture point no 2. So, How can I get all points in an excel to one cell of a data frame?

The data which I get looks like:

  1. There is a book.

The data which I want should look like:

  1. There is a book. 2. There is also a pen along with the book. 3. So, I decided to study for a while.

Upvotes: 0

Views: 38

Answers (1)

Abhay
Abhay

Reputation: 615

I created an excel file with a column named remarks which looks like below:

   remarks
0  1. There is a book.
   2. There is also a pen along with the book.
   3. So, I decided to study for a while.

Here, I have entered all the text mentioned in your question into single cell.

import pandas as pd
df = pd.read_excel('remarks.xlsx')

Now when I try to print the column remarks it gives:

df['remarks']

0    1. There is a book.\n2. There is also a pen al...
Name: A, dtype: object

To solve your problem try:

df['remarks_without_linebreak'] = df['remarks'].replace('\n',' ', regex=True)

If you print the row in the column 'remarks_without_linebreak' you will get the result as you want

Upvotes: 1

Related Questions