简体   繁体   中英

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

import pandas as pd

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

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

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

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

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

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

Assign column names and set columns in the mean time :

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

在此输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM