pandas创建series有哪些方法,常见操作怎样实现
Admin 2022-09-05 群英技术资讯 762 次浏览
print("====创建series方法一===") dic={"a":1,"b":2,"c":3,"4":4} s=pd.Series(dic) print(s)
由字典创建,字典的key就是index,values就是valuse
key肯定是字符串,假如values类型不止一个会怎么样? → dic = {‘a’:1 ,‘b’:‘hello’ , ‘c’:3, ‘4’:4, ‘5’:5}
由数组创建(一维数组)
arr=np.random.rand(5) s=pd.Series(arr) print(arr) print(s) #默认index是从0开始,步长为1的数字 s=pd.Series(arr,index=['a','b','c','d','e'],dtype=np.object) print(s)
由标量创建
s=pd.Series(10,index=range(4)) print(s)
使用
from pandas import Series
a. 常规创建
>>> obj = Series([1,2,3], index=['A','B','C']) >>> obj A 1 B 2 C 3 dtype: int64
b. 根据字典创建
>>> obj = Series({'a':1,'b':2,'c':3}) >>> obj a 1 b 2 c 3 dtype: int64
c. Series嵌套Series
>>> obj1 = Series([1,2,3],index=['a','b','c']) >>> obj2 = Series([4,5,6],index=['d','e','f']) >>> obj3 = Series([obj1, obj2],index=['name1', 'name2']) >>> obj3 name1 a 1 b 2 c 3 dtype: int64 name2 d 4 e 5 f 6 dtype: int64 dtype: object
>>> obj1 = Series([1,2,3],index=['a','b','c']) >>> obj1 a 1 b 2 c 3 dtype: int64 >>> obj1.append(Series([4,5],index=['d','e'])) a 1 b 2 c 3 d 4 e 5 dtype: int64
如果是嵌套的Series的追加
>>> obj1 = Series([1,2,3],index=['a','b','c']) >>> obj1 a 1 b 2 c 3 dtype: int64 >>> obj1.drop('b') a 1 c 3 dtype: int64
>>> obj1 = Series([1,2,3],index=['a','b','c']) >>> obj1 a 1 b 2 c 3 dtype: int64 >>> obj1.a = -1 >>> obj1['b'] = -2 >>> obj1 a -1 b -2 c 3 dtype: int64
>>> obj1 = Series([1,2,3],index=['a','b','c']) >>> obj1 a 1 b 2 c 3 dtype: int64 >>> print(obj1.a == 1) True
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了python数据操作之lambda表达式详情,文章基于python的相关资料展开lambda表达式具体的内容,感兴趣的小伙伴可以参考一下
这篇文章主要为大家详细介绍了python链表的基础概念和基础用法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
需求背景最近为公司开发了一套邮件日报程序,邮件一般就是表格,图片,然后就是附件。附件一般都是默认写到txt文件里,但是PM希望邮件里的
本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于实现定时任务的相关问题,可以使用第三方包来管理定时任务,相对来说apscheduler使用起来更简单,下面一起来看一下使用的方法,希望对大家有帮助。
本文主要给大家分享关于使用python实现查找算法的介绍,包括顺序查找、二分查找、插值查找、斐波那契查找、树表查找、分块查找、哈希查找这七种查找算法的实现,具有一定的借鉴价值,感兴趣的朋友可以参考学习。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008