10

Using the following code:

predictions  = pd.DataFrame([x6,x5,x4,x3,x2,x1])
print(predictions)

Prints the following in the console:

            0
0  782.367392
1  783.314159
2  726.904744
3  772.089101
4  728.797342
5  753.678877

How do I print predictions without row (0-5) or column (0) indexes?

In R the code would look like:

print(predictions, row.names = FALSE)
5
  • 2
    You can simply use as print(predictions.to_string(index=False))
    – niraj
    Commented Sep 19, 2018 at 0:59
  • 1
    @student has the right answer
    – James
    Commented Sep 19, 2018 at 1:00
  • 1
    You nailed it @student
    – ZJAY
    Commented Sep 19, 2018 at 1:04
  • And I thought Python syntax was simpler than R :)
    – ZJAY
    Commented Sep 19, 2018 at 1:04
  • Great it worked! I did not add answer since it was already answered. Happy Coding. :)
    – niraj
    Commented Sep 19, 2018 at 1:06

3 Answers 3

16
print(df.to_string(index=False, header=False))
1
  • 2
    That looks nice. You could also do some rounding before printing, e.g. df.round(2).to_string(...)
    – Alexander
    Commented Sep 19, 2018 at 1:21
4
print(predictions.values.tolist())

Or

df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))

>>> for row in df.values: print(row)
[-1.09989127 -0.17242821 -0.87785842]
[ 0.04221375  0.58281521 -1.10061918]
[ 1.14472371  0.90159072  0.50249434]
[ 0.90085595 -0.68372786 -0.12289023]
[-0.93576943 -0.26788808  0.53035547]
2
  • That returns: [[782.3673915172626], [783.314158724909], [726.9047442598429], [772.0891010290351], [728.7973416236138], [753.67887656913]] - I'm not looking to return brackets or commas, just numbers
    – ZJAY
    Commented Sep 19, 2018 at 0:58
  • I just answered this related question about 10 mins ago. stackoverflow.com/questions/52396315/…
    – Alexander
    Commented Sep 19, 2018 at 1:00
2

Or use:

predictions[0].tolist()

Or can do something like:

'\n'.join(map(str,predictions[0].tolist()))

Or can do:

for i in str(df).splitlines()[1:]:
    print(i.split(None,1)[1])

Or want to assign to string:

s=''
for i in str(df).splitlines()[1:]:
    s+=i.split(None,1)[1]+'\n'
print(s.rstrip())
2
  • This still has commas, anyway to just simply print the numbers in the dataframe?
    – ZJAY
    Commented Sep 19, 2018 at 1:00
  • @ZJAY Then do, '\n'.join(map(str,predictions[0].tolist())) Commented Sep 19, 2018 at 1:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.