14

Good day,

Is it possible to get multi line cell output when using pandas DataFrame in a shell? I understand the whole row will have height more than 1 char, but that's what I actually want.

Example:

data = [
       {'id': 1, 't': 'very long text\ntext line 2\ntext line 3'},
       {'id': 2, 't': 'short text'}
       ]
df = pd.DataFrame(data)
df.set_index('id', inplace=True)
print(df)

and want to get output:

id                  t
1      very long text
       text line 2
       text line 3
2      short text

instead of

id                                         t
1     very long text\ntextline 2\ntext line3
2                                 short text
disserman
  • 292
  • 2
  • 3
  • 7
  • 2
    You can use Python's tabulate https://pypi.org/project/tabulate/ to solve the problem: from tabulate import tabulate; html_table = tabulate(df, df.columns, tablefmt='html') – Jirka Apr 15 '21 at 16:02

3 Answers3

12

Look at this answer from @unsorted here Pretty printing newlines inside a string in a Pandas DataFrame

here it is with you example:

import pandas as pd
from IPython.display import display, HTML

def pretty_print(df):
    return display(HTML(df.to_html().replace("\\n","<br>")))

data = [
       {'id': 1, 't': 'very long text\ntext line 2\ntext line 3'},
       {'id': 2, 't': 'short text'}
       ]
df = pd.DataFrame(data)
df.set_index('id', inplace=True)

pretty_print(df)

         t
id  
1   very long text
    text line 2
    text line 3
2   short text
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
8

You will get your expected result with

df.t.str.split("\n", expand=True).stack()
J. Doe
  • 3,458
  • 2
  • 24
  • 42
2

You can unnest your string with Series after str.split

df.set_index('id').t.str.split(r'\n').apply(pd.Series).stack().reset_index(level=1,drop=True).to_frame('t')
Out[177]: 
                 t
id                
1   very long text
1      text line 2
1      text line 3
2       short text
BENY
  • 317,841
  • 20
  • 164
  • 234