Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print each line from multiline text in pandas dataframe in python?

I have a dataset in which there is a column named "Mobile Features". when printed each row this column has multiline text and prints all the text at a time.

How to print each line from the multiline text

df

  Mobile_Name Mobile_Price Mobile_Features
1. Realme       25000       54 MP Camera
                            12 GB RAM
                            750 Snapdragon Processor
                            Best in Class
                            4.5 rating / 5

2. Celkon       18000      45 MP Camera
                           8 GB RAM
                           750 Snapdragon Processor
                           Best in Class
                           4.7 rating / 5


for each_row in df['Mobile_Features']:
    for i in each_row:
         print(i)

But it prints each character instead of one single line.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How to print a single line? It would be great if someone can help me. Thank You.

>Solution :

You need to split the data in each_row by newline (\n):

for each_row in df['Mobile_Features']:
    for i in each_row.split('\n'):
        print(i)

or

for each_row in df['Mobile_Features'].str.split('\n'):
    for i in each_row:
        print(i)

Output (for either code for your sample data):

54 MP Camera
12 GB RAM
750 Snapdragon Processor
Best in Class
4.5 rating / 5
45 MP Camera
8 GB RAM
750 Snapdragon Processor
Best in Class
4.7 rating / 5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading