網(wǎng)站首頁 編程語言 正文
pandas創(chuàng)建series方法
print("====創(chuàng)建series方法一===")
dic={"a":1,"b":2,"c":3,"4":4}
s=pd.Series(dic)
print(s)
創(chuàng)建方法一
由字典創(chuàng)建,字典的key就是index,values就是valuse
key肯定是字符串,假如values類型不止一個(gè)會怎么樣? → dic = {‘a(chǎn)’:1 ,‘b’:‘hello’ , ‘c’:3, ‘4’:4, ‘5’:5}
Series 創(chuàng)建方法二
由數(shù)組創(chuàng)建(一維數(shù)組)
arr=np.random.rand(5)
s=pd.Series(arr)
print(arr)
print(s)
#默認(rèn)index是從0開始,步長為1的數(shù)字
s=pd.Series(arr,index=['a','b','c','d','e'],dtype=np.object)
print(s)
Series 創(chuàng)建方法三
由標(biāo)量創(chuàng)建
s=pd.Series(10,index=range(4))
print(s)
Pandas的Series常用方法
使用
from pandas import Series
1. 創(chuàng)建Series
a. 常規(guī)創(chuàng)建
>>> obj = Series([1,2,3], index=['A','B','C'])
>>> obj
A ? ?1
B ? ?2
C ? ?3
dtype: int64
b. 根據(jù)字典創(chuàng)建
>>> 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的追加
- 錯(cuò)誤寫法: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
相關(guān)推薦
- 2022-06-02 Python?turtle編寫簡單的球類小游戲_python
- 2022-12-07 C語言內(nèi)存分布與heap空間分別詳細(xì)講解_C 語言
- 2022-12-24 Docker網(wǎng)絡(luò)及容器通信原理詳解_docker
- 2024-02-28 UNI-APP,動(dòng)態(tài)設(shè)置view的背景圖片
- 2022-05-13 C++ OpenCV cv::Mat 矩陣操作
- 2022-07-02 C++精要分析decltype的作用及用法_C 語言
- 2023-10-16 向前端傳遞Long類型數(shù)據(jù)時(shí)發(fā)生精度缺失解決辦法
- 2023-10-14 【c++】四舍五入
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支