Edu Galindo
Edu Galindo

Reputation: 165

How to create a dataframe from numpy arrays?

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

Upvotes: 3

Views: 7678

Answers (7)

LaSul
LaSul

Reputation: 2411

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

Upvotes: 3

timgeb
timgeb

Reputation: 78650

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

Upvotes: 4

Parth Verma
Parth Verma

Reputation: 820

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)

Upvotes: 1

trsvchn
trsvchn

Reputation: 8981

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

Upvotes: 2

Tim
Tim

Reputation: 2843

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

Upvotes: 1

AmourK
AmourK

Reputation: 732

import pandas as pd

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

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

Upvotes: 4

Allen
Allen

Reputation: 246

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'])

Upvotes: 3

Related Questions