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
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
You will get your expected result with
df.t.str.split("\n", expand=True).stack()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With