3

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

7 Answers 7

4

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), ...

Sign up to request clarification or add additional context in comments.

Comments

4
import pandas as pd

df = pd.DataFrame({'x':x, 'y':y})

Change the key in the dictionary to your desired column name.

Comments

4

Assign column names and set columns in the mean time :

import pandas as pd
df = pd.DataFrame({"x" : x , "y" : y})

enter image description here

Comments

3

You can do the following.

import pandas as pd
import numpy as np

df = pd.DataFrame()
df['x'] = np.linspace(0,50)
df['y'] = np.exp(df['x'])

Comments

2

Simply:

Code:

import pandas as pd
import numpy as np

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

df = pd.DataFrame({'x': x, 'y': y})

Comments

1

Just make a list of tuples and pass it to the DataFrame constructor:

df = pd.DataFrame([(i, np.exp(i)) for i in np.linspace(0,50)], columns=['x', 'y'])

Output

x        y
0  1.000000e+00
1  2.718282e+00
2  7.389056e+00
...

1 Comment

Besides being inefficient because of the loop, np.arange(50) does not produce the same output as np.linspace(0, 50).
1

What you are looking for is [np.concatenate][1].

So for your example, the code would be

import numpy as np
x = np.linspace(0,50)
y = np.exp(x)
z = np.concatenate((x.reshape(1,-1),y.reshape(1,-1))).T
print(z.shape)
# (2,50)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.