網站首頁 編程語言 正文
pandas創建series方法
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}
Series 創建方法二
由數組創建(一維數組)
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)
Series 創建方法三
由標量創建
s=pd.Series(10,index=range(4))
print(s)
Pandas的Series常用方法
使用
from pandas import Series
1. 創建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
2. Series追加
>>> 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的追加
- 錯誤寫法:obj['name1'].append(Series([1], index = ['a']));
- 正確寫法:obj.append(Series([Series([1], index = ['a'])], index = ['name1']))
3. 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
4. Series改
>>> 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
5. Series查
>>> obj1 = Series([1,2,3],index=['a','b','c'])
>>> obj1
a ? ?1
b ? ?2
c ? ?3
dtype: int64
>>> print(obj1.a == 1)
True
原文鏈接:https://blog.csdn.net/weixin_46457946/article/details/109911807
相關推薦
- 2022-09-29 C#/VB.NET中從?PDF?文檔中提取所有表格_C#教程
- 2022-08-15 Android開發gradle拉取依賴的加速配置_Android
- 2022-11-23 Python面向對象的內置方法梳理講解_python
- 2022-04-09 SpringBoot 提示:RequestRejectedException:The request
- 2022-09-26 Python?Celery定時任務詳細講解_python
- 2023-05-30 docker如何查看容器啟動命令(已運行的容器)_docker
- 2022-06-08 ASP.NET?Core使用IHttpClientFactory發出HTTP請求_基礎應用
- 2022-11-21 詳解React獲取DOM和獲取組件實例的方式_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支