Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas dataframe and multi line values [duplicate]

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
like image 379
disserman Avatar asked Jun 01 '18 13:06

disserman


3 Answers

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
like image 153
It_is_Chris Avatar answered Nov 15 '22 04:11

It_is_Chris


You will get your expected result with

df.t.str.split("\n", expand=True).stack()
like image 8
J. Doe Avatar answered Nov 15 '22 04:11

J. Doe


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
like image 2
BENY Avatar answered Nov 15 '22 02:11

BENY