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

Time:10-17

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.

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

CodePudding user response:

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
  • Related