在 Jupyter 笔记本中的表格中获取输出 - Python

时间:2021-03-23 14:20:42

标签: python jupyter-notebook

我需要在表格中的 jupyter notebook 代码中获取输出。 这是我的代码。

import numpy as np

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

我得到如下输出。

enter image description here

我需要在列名为“x”和“y”的表中使用它。当我向下滚动时,列名应该是可见的。

谢谢。

1 个答案:

答案 0 :(得分:1)

我认为,最干净的方法是使用熊猫数据框如下:

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())

会给你

   x   y
0  1   5
1  2   7
2  3   9
3  4  11
4  5  13