1

I need to get output in my Jupyter notebook code in a table. Here is my code.

import numpy as np

for x in range(1,1000):
    y=2*x+3
    print(x,y)

I get the output like below.

enter image description here

I need it in a table with column names "x" and "y". When I scroll down, the column names should be visible.

1 Answer 1

1

I think that, the cleanest way is to use pandas dataframe as follow:

import numpy as np
import pandas as pd

table=[]
for x in range(1,1000):
    y=2*x+3
    table.append([x,y])

df= pd.DataFrame(np.array(table_x),columns = ['x','y'])

print(df.head())

will give you

   x   y
0  1   5
1  2   7
2  3   9
3  4  11
4  5  13
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. But I get only first 5 values.
yes, print df.head will give you only the first 5 values. To print all values you can use the solution described here: stackoverflow.com/questions/19124601/…
Thank you. I will look in to that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.