当前位置: 代码迷 >> 综合 >> 20200721-1-numpy和pandas生成矩阵
  详细解决方案

20200721-1-numpy和pandas生成矩阵

热度:5   发布时间:2024-01-30 23:08:35.0
import numpy as np
import pandas as pd#pandas的Series
s = pd.Series([1,2,'花朵',98,1,20,0])
print(s)
datas = pd.date_range('20200721',periods=6)#通过numpy生成的数组,传递给pandas
df = pd.DataFrame(np.random.randn(6,4),index=datas,columns=['a','b','c','d'])
print(df)
df1 = pd.DataFrame(np.random.randn(6,4))#直接生成pandas数组
df2 = pd.DataFrame({'小明列':22,'B':pd.Timestamp('20200620'),'C':pd.Series(1,index=list(range(4)),dtype='float32'),'D':np.array([3]*4,dtype='int32'),'E':pd.Categorical(['hello','mike','momo','train']),'F':'foolie'})

s
Out[9]: 
0     1
1     2
2    花朵
3    98
4     1
5    20
6     0
dtype: object

 

df
Out[8]: 
a         b         c         d
2020-07-21 -1.334143 -0.418217  0.399811  1.286937
2020-07-22  0.122256  1.625069  0.064293 -0.485579
2020-07-23  1.356831 -0.575917  0.326547 -1.440079
2020-07-24 -1.075300  0.248529  1.921025 -0.503600
2020-07-25 -0.965514  0.175607 -0.180204 -2.009705
2020-07-26 -0.036789 -0.266677  0.091262  0.823388

 

 

df1
Out[5]: 
0         1         2         3
0 -0.221695  0.469178 -1.385129 -0.579727
1 -0.388149 -1.804306 -0.811451  1.867572
2  1.400014 -0.386663  0.935390  0.062821
3  1.000829  0.279782 -0.326873 -0.530542
4  0.862546  0.776912 -0.349843 -0.773228
5 -0.215625 -1.157521 -0.855164  0.265402

 

df2
Out[7]: 
小明列          B    C  D      E       F
0   22 2020-06-20  1.0  3  hello  foolie
1   22 2020-06-20  1.0  3   mike  foolie
2   22 2020-06-20  1.0  3   momo  foolie
3   22 2020-06-20  1.0  3  train  foolie

  相关解决方案