- A+

Category：Languages

I am trying to create a matrix / DataFrame with the numbers stored in 2 variables

`x = np.linspace(0,50) y = np.exp(x) `

and I would like them to look like this:

`x | y ___________________ 0 | 1.0... 1 | 2.77... 2 | 7.6... ... | ... 50 | 5.18e+21... `

I would like it to be in a `DataFrame`

so I can work with it with the `pandas`

library.

Thanks in advance

With `pandas`

:

You can issue

`>>> xs = np.arange(51) >>> ys = np.exp(xs) `

to get the x and y values and then build your dataframe with

`>>> df = pd.DataFrame({'x': xs, 'y': ys}) >>> df x y 0 0 1.000000e+00 1 1 2.718282e+00 2 2 7.389056e+00 3 3 2.008554e+01 ... `

In this case, you can also use the x-values as the index of a series without losing any information.

`>>> index = pd.RangeIndex(0, 51, name='x') >>> exps = pd.Series(data=np.exp(index), index=index, name='y') >>> exps x 0 1.000000e+00 1 2.718282e+00 2 7.389056e+00 3 2.008554e+01 ... Name: y, dtype: float64 `

Without `pandas`

:

Consider if you truly need a dataframe or series. You could just leave it at

`>>> xs = np.arange(51) >>> ys = np.exp(xs) `

and then index into `ys`

with the integers `0`

, `1`

, `2`

, ... to get the values of `exp(0)`

, `exp(1)`

, `exp(2)`

, ...