pandas知识点(数据结构)
2018-11-22 08:45:33来源:博客园 阅读 ()
In [3]: obj = Series([1,2,3,4,5]) In [4]: obj Out[4]: 0 1 1 2 2 3 3 4 4 5 dtype: int64 In [5]: obj.values Out[5]: array([1, 2, 3, 4, 5], dtype=int64) In [6]: obj.index Out[6]: RangeIndex(start=0, stop=5, step=1)
创建对各个数据点进行标记的索引:
In [7]: obj2 = Series([4,1,9,7], index=["a","c","e","ff"]) In [8]: obj2 Out[8]: a 4 c 1 e 9 ff 7 dtype: int64 In [9]: obj2.index Out[9]: Index(['a', 'c', 'e', 'ff'], dtype='object')
取一个值或一组值:
In [10]: obj2["c"] Out[10]: 1 In [11]: obj2[["c","e"]] Out[11]: c 1 e 9 dtype: int64
数组运算,会显示索引:
In [12]: obj2[obj2>3] Out[12]: a 4 e 9 ff 7 dtype: int64
In [13]: "c" in obj2 Out[13]: True
In [14]: data = {"name":"liu","year":18,"sex":"man"} In [15]: obj3 = Series(data) In [16]: obj3 Out[16]: name liu year 18 sex man dtype: object
In [17]: list1 = ["name","year","mobile"] In [18]: obj4 = Series(data,index=list1) In [19]: obj4 Out[19]: name liu year 18 mobile NaN dtype: object
PS:因为data字典中没有mobile所以值为NaN
In [20]: pd.isnull(obj4) Out[20]: name False year False mobile True dtype: bool In [21]: pd.notnull(obj4) Out[21]: name True year True mobile False dtype: bool In [22]: obj4.isnull() Out[22]: name False year False mobile True dtype: bool In [23]: obj4.notnull() Out[23]: name True year True mobile False dtype: bool
In [7]: obj4.name = "hahaha" In [8]: obj4.index.name = "state" In [9]: obj4 Out[9]: state name liu year 18 mobile NaN Name: hahaha, dtype: object
In [13]: data = { "state":[1,1,2,1,1], "year":[2000,2001,2002,2004,2005], "pop":[1.5,1.7,3.6,2.4,2.9] } In [14]: frame = DataFrame(data) In [15]: frame Out[15]: state year pop 0 1 2000 1.5 1 1 2001 1.7 2 2 2002 3.6 3 1 2004 2.4 4 1 2005 2.9
In [18]: frame2 = DataFrame( data, columns=["year","state","pop","debt"], index=["one","two","three","four","five"] ) In [19]: frame2 Out[19]: year state pop debt one 2000 1 1.5 NaN two 2001 1 1.7 NaN three 2002 2 3.6 NaN four 2004 1 2.4 NaN five 2005 1 2.9 NaN
In [7]: frame2.year Out[7]: one 2000 two 2001 three 2002 four 2004 five 2005 Name: year, dtype: int64
PS:返回的索引不变,且name属性被设置了
In [11]: frame2.loc["three"] Out[11]: year 2002 state 2 pop 3.6 debt NaN Name: three, dtype: object
In [12]: frame2['debt'] = 16.5 In [13]: frame2 Out[13]: year state pop debt one 2000 1 1.5 16.5 two 2001 1 1.7 16.5 three 2002 2 3.6 16.5 four 2004 1 2.4 16.5 five 2005 1 2.9 16.5
In [17]: val = Series([1.2,1.5,1.7], index=["two","four","five"]) In [18]: frame2['debt'] = val In [19]: frame2 Out[19]: year state pop debt one 2000 1 1.5 NaN two 2001 1 1.7 1.2 three 2002 2 3.6 NaN four 2004 1 2.4 1.5 five 2005 1 2.9 1.7
In [21]: frame2["eastern"] = frame2.state == 1 In [22]: frame2 Out[22]: year state pop debt eastern one 2000 1 1.5 NaN True two 2001 1 1.7 1.2 True three 2002 2 3.6 NaN False four 2004 1 2.4 1.5 True five 2005 1 2.9 1.7 True
In [23]: dic = {"name":{"one":"liu","two":"rui"},"year":{"one":"23","two":"22"}} In [24]: frame3 = DataFrame(dic) In [25]: frame3 Out[25]: name year one liu 23 two rui 22
In [26]: frame3.index.name = "index" In [27]: frame3.columns.name = "state" In [28]: frame3 Out[28]: state name year index one liu 23 two rui 22
In [29]: frame3.values Out[29]: array([['liu', '23'], ['rui', '22']], dtype=object)
In [30]: obj = Series(range(3),index=["a","b","c"]) In [31]: index = obj.index In [32]: index Out[32]: Index(['a', 'b', 'c'], dtype='object')
In [35]: index = pd.Index(np.arange(3)) In [36]: obj2 = Series([1.5,0.5,2],index=index) In [37]: obj2.index is index Out[37]: True
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:操作文件方法
- 网络编程相关知识点 2019-08-13
- Python 数据结构理解分享 2019-07-24
- django修改表数据结构后报错的解决办法 2019-07-24
- Python-04-数据结构 2019-07-24
- python算法与数据结构-二叉树的代码实现(46) 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash