0

I have a simple question that I could not find anywhere the solution. I'm sorry if this is already asked here.

My code is simple:

s = pd.Series(np.random.rand(150))

I just want to print it and see all of its output:

print(s)

The output:

>>> print(s)
0      0.580686
1      0.508062
2      0.184946
3      0.275074
4      0.831654
5      0.080571
6      0.153813
7      0.088002
8      0.082935
9      0.513361
10     0.064965
11     0.325820
12     0.933384
13     0.605428
14     0.121554
15     0.767311
16     0.476030
17     0.423448
18     0.028080
19     0.905429
20     0.185212
21     0.770636
22     0.569428
23     0.294643
24     0.482404
25     0.217303
26     0.406958
27     0.893210
28     0.863044
29     0.270612
         ...   
120    0.221621
121    0.077798
122    0.154689
123    0.155532
124    0.828792
125    0.564565
126    0.714583
127    0.913120
128    0.632600
129    0.115126
130    0.543111
131    0.420206
132    0.135349
133    0.912911
134    0.788410
135    0.685158
136    0.282509
137    0.545654
138    0.074851
139    0.247384
140    0.244246
141    0.798268
142    0.348548
143    0.775883
144    0.114062
145    0.246298
146    0.216241
147    0.639255
148    0.902388
149    0.416965
Length: 150, dtype: float64

As you can see it is dividing my output from line 29 to 120. I want the whole output to be written.

I've tried this in the Ubuntu terminal, IPython Console 6.5.0, both running as a saved file & running on the interactive shells.

Then, I wondered what if I wanted the output in a file:

import pandas as pd
import numpy as np

with open('test.txt', 'w') as myfile:
    s = pd.Series(np.random.randn(150))
    myfile.write(str(s))

.. I checked the test.txt. Output is still the same, divided with '...'.

How can I make python to print all of the output?

teoman
  • 959
  • 2
  • 10
  • 21
  • Use `s.to_csv('test.txt', index=False)` – jezrael Aug 31 '18 at 08:44
  • It's "convenience" formatting so if you print *massive* frames/series/arrays etc... you don't sit and wait hours for your computer to display it... `myfile.write(s.to_string())` will write to the file the *full* string for your series.... – Jon Clements Aug 31 '18 at 08:45
  • Um... think there's also a good Q/A on `pd.set_option` regarding this as well... – Jon Clements Aug 31 '18 at 08:46
  • @JonClements I see. Thanks for clarifying. – teoman Aug 31 '18 at 08:47
  • @Teoman imagine if you had a 400 column 1 million row dataframe and you printed that... you'd not get your computer back for a bit and you wouldn't want to see all that anyway... So it works on the basis if you do want that, you have to be really explicit about it... It'll try and print the first N and last N (with the ...) between and the first N columns and last N columns as above... so you get an idea of what's there... Generally setting the number of columns to None (or really high number) so you can scroll across columns makes sense but you'll rarely want to see all rows :) – Jon Clements Aug 31 '18 at 08:50
  • @JonClements Well, probably that was the best case and a good reason for the developers to think that way, I personally think that If I would want to see all the output I just print it, and I would give it an argument if I wanted it to crop my output. It is my personal idea though since I am a real beginner and do not know the core reason behind these. – teoman Aug 31 '18 at 09:06
  • Sure... for a few hundred rows there's little harm... but as soon as you hit significant numbers and you don't *think* there is you're basically going to have either wait for ages and get so much on screen it won't fit in your terminal/display buffers so it'll scroll by and be lost anyway or you'll think something's gone wrong... neither is useful plus you'll likely have to end up interrupting your process to it... – Jon Clements Aug 31 '18 at 09:11
  • Think of it this way... if you had a 20gb file and wanted to have a look at it... would you `cat` the file to screen... or would you do `head -10`, `tail -10` or use `less` which can paginate and not try and show you everything at once? – Jon Clements Aug 31 '18 at 09:11
  • @JonClements, Of course, I would not want to `cat` the file immediately or `less` a file which is that big. I can see the point why Python does this now. – teoman Aug 31 '18 at 11:20

0 Answers0