Given following DF
JavaScript
x
6
1
0
2
0 Maintain the distance.
3
1 Ensure it is checked.
4
2 Inbetween distance must be 0.5.
5
3 Take extra notes of the distance.
6
Is it possible for Pandas to join all rows, give a new line and return the result as a variable?
Final output:
Variable n
:
JavaScript
1
8
1
Maintain the distance.
2
3
Ensure it is checked.
4
5
Inbetween distance must be 0.5.
6
7
Take extra notes of the distance.
8
I have explored str.cat()
but seem like the separator do not allow a n
.
Advertisement
Answer
Use as many n
newlines as you like.
JavaScript
1
24
24
1
>>> df = pd.DataFrame(dict(word='a man a plan a canal'.split()))
2
>>> df
3
word
4
0 a
5
1 man
6
2 a
7
3 plan
8
4 a
9
5 canal
10
>>>
11
>>> text = df.word.str.cat(sep='nn')
12
>>> print(text)
13
a
14
15
man
16
17
a
18
19
plan
20
21
a
22
23
canal
24