栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

numpy&pandas(一)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

numpy&pandas(一)

构建:

pd.Series构建单列数据框,附带索引,np.nan表示空值

import numpy as np
import pandas as  pd

s=pd.Series([1,2,3,np.nan,4,5])
print(s)

结果:

0    1.0
1    2.0
2    3.0
3    NaN
4    4.0
5    5.0
dtype: float64

构建:

pd.date_range构建日期数据框,周期和单位可选,'20201024'表示开始时间,freq=‘D’、'M'、'Y'表示日月年,默认为D

dates=pd.date_range('20201024',periods=6,freq='D')
print(dates)

结果:

DatetimeIndex(['2020-10-24', '2020-10-25', '2020-10-26', '2020-10-27',
               '2020-10-28', '2020-10-29'],
              dtype='datetime64[ns]', freq='D')

构建:

构建数据框,以及索引和列名,index表示索引,columns表示列名,np.random.randn(6,4)表示六行四列的正态分布矩阵

np.arange(12).reshape((3,4))表示对0-11进行三行四列的排列

df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
df1=pd.DataFrame(np.arange(12).reshape((3,4)))
df2=pd.DataFrame({'A':1.,
                'B':pd.Timestamp('20130102'),
                'C':pd.Series(1,index=list(range(4)),dtype='float32'),
                'D':np.array(([3]*4),dtype='int32'),
                'E':pd.Categorical(['test','train','test','train']),
                'F':'fool'})
print(df)

print(df1)

print(df2)

结果:

                   a         b         c         d
2020-10-24 -1.064660 -0.228393 -1.897609  0.566754
2020-10-25  0.277156  1.308101  0.292550 -0.786283
2020-10-26 -0.090731  0.890201 -0.169900 -0.068995
2020-10-27  1.411326  0.943571  0.797507  1.352421
2020-10-28 -0.403858  1.016165 -0.784167 -2.197176
2020-10-29  0.809139  0.278650  1.226037 -0.384152

   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11



     A          B    C  D      E     F
0  1.0 2013-01-02  1.0  3   test  fool
1  1.0 2013-01-02  1.0  3  train  fool
2  1.0 2013-01-02  1.0  3   test  fool
3  1.0 2013-01-02  1.0  3  train  fool

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/858554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号