Showing new lines(n) in PD Dataframe String
Question:
I am trying to create a dataframe that has one column that contains a string of texts that has lines. Those lines are separated by lines so that they are easily readable.
string=['''
hello all:. I request the following from you:
do the laundry:
sleep early
wake up early
feed the chicken
''']
ii=pd.DataFrame(string, columns=['requirement'])
ii
However, when I generate the dataframe, the table shows very messy and all the new lines are separated by (n) in text format.
nhello all:. I request the following from you:nnndo the laundry:nsleep earlynwake up earlynfeed the chickennn
How do I preserve the lines inside of the PD Dataframe so that it is easily readable?
Answers:
A robust way is to use tabulate
:
# pip install tabulate
from tabulate import tabulate
print(tabulate(ii, tablefmt='plain', headers=list(ii)))
Output:
requirement
0 hello all:. I request the following from you:
do the laundry:
sleep early
wake up early
feed the chicken
Other example:
ii = pd.DataFrame({'A': ['123nABCDEnnnF', '12n3'],
'B': [1, 2],
'C': ['ABC', 'AnBnC']})`
Output:
A B C
0 123 1 ABC
ABCDE
F
1 12 2 A
3 B
C
as HTML
from IPython.display import display, HTML
display(HTML(ii.to_html().replace('\n', '<br>')))
Output:
example 1:
example 2:
alignment
print(tabulate(ii, tablefmt='plain', headers=list(ii), stralign='left'))
A B C
0 123 1 ABC
ABCDE
F
1 12 2 A
3 B
C
print(tabulate(ii, tablefmt='plain', headers=list(ii), stralign='right'))
A B C
0 123 1 ABC
ABCDE
F
1 12 2 A
3 B
C
you can use:
ii['requirement'].replace('n', '', regex=True, inplace=True)
You can use:
import re
string=['''
hello all:. I request the following from you:
do the laundry:
sleep early
wake up early
feed the chicken
''']
string = [re.sub('(n+)(s+)',' ',s).strip() for s in string]
ii=pd.DataFrame(string, columns=['requirement'])
ii
I am trying to create a dataframe that has one column that contains a string of texts that has lines. Those lines are separated by lines so that they are easily readable.
string=['''
hello all:. I request the following from you:
do the laundry:
sleep early
wake up early
feed the chicken
''']
ii=pd.DataFrame(string, columns=['requirement'])
ii
However, when I generate the dataframe, the table shows very messy and all the new lines are separated by (n) in text format.
nhello all:. I request the following from you:nnndo the laundry:nsleep earlynwake up earlynfeed the chickennn
How do I preserve the lines inside of the PD Dataframe so that it is easily readable?
A robust way is to use tabulate
:
# pip install tabulate
from tabulate import tabulate
print(tabulate(ii, tablefmt='plain', headers=list(ii)))
Output:
requirement
0 hello all:. I request the following from you:
do the laundry:
sleep early
wake up early
feed the chicken
Other example:
ii = pd.DataFrame({'A': ['123nABCDEnnnF', '12n3'],
'B': [1, 2],
'C': ['ABC', 'AnBnC']})`
Output:
A B C
0 123 1 ABC
ABCDE
F
1 12 2 A
3 B
C
as HTML
from IPython.display import display, HTML
display(HTML(ii.to_html().replace('\n', '<br>')))
Output:
example 1:
example 2:
alignment
print(tabulate(ii, tablefmt='plain', headers=list(ii), stralign='left'))
A B C
0 123 1 ABC
ABCDE
F
1 12 2 A
3 B
C
print(tabulate(ii, tablefmt='plain', headers=list(ii), stralign='right'))
A B C
0 123 1 ABC
ABCDE
F
1 12 2 A
3 B
C
you can use:
ii['requirement'].replace('n', '', regex=True, inplace=True)
You can use:
import re
string=['''
hello all:. I request the following from you:
do the laundry:
sleep early
wake up early
feed the chicken
''']
string = [re.sub('(n+)(s+)',' ',s).strip() for s in string]
ii=pd.DataFrame(string, columns=['requirement'])
ii