简体   繁体   English

如何从numpy数组创建数据框?

[英]How to create a dataframe from numpy arrays?

I am trying to create a matrix / DataFrame with the numbers stored in 2 variables 我正在尝试创建一个矩阵/ DataFrame,其中数字存储在2个变量中

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. 我希望它在一个DataFrame所以我可以使用pandas库。

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 : 有了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 获取x和y值,然后使用。来构建数据框

>>> 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. 在这种情况下,您还可以使用x值作为系列的索引而不会丢失任何信息。

>>> 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 : 没有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) , ... 然后索引ys与整数012 ,...得到的值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: 只需创建一个元组列表并将其传递给DataFrame构造函数:

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] . 你要找的是[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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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