pandas創(chuàng)建series的三種方法小結(jié)
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è)會(huì)怎么樣? → dic = {‘a’: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開始,步長(zhǎng)為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: int64c. 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: int644. 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
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
pandas數(shù)據(jù)處理之繪圖的實(shí)現(xiàn)
這篇文章主要介紹了pandas數(shù)據(jù)處理之繪圖的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Python監(jiān)控主機(jī)是否存活并以郵件報(bào)警
本文是利用python腳本寫的簡(jiǎn)單測(cè)試主機(jī)是否存活,此腳本有個(gè)缺點(diǎn)不適用線上,由于網(wǎng)絡(luò)延遲、丟包現(xiàn)象會(huì)造成誤報(bào)郵件,感興趣的朋友一起看看Python監(jiān)控主機(jī)是否存活并以郵件報(bào)警吧2015-09-09
python算法學(xué)習(xí)雙曲嵌入論文代碼實(shí)現(xiàn)數(shù)據(jù)集介紹
由于雙曲嵌入相關(guān)的文章已經(jīng)有了一系列的代碼。本篇博客主要目的實(shí)現(xiàn)最開始的雙曲嵌入論文,將論文中有些直接寫出來的內(nèi)容進(jìn)行了細(xì)節(jié)的推導(dǎo),同時(shí)實(shí)現(xiàn)對(duì)應(yīng)的代碼2021-11-11
Python打包文件執(zhí)行報(bào)錯(cuò):ModuleNotFoundError: No module 
這篇文章給大家介紹了Python打包文件執(zhí)行報(bào)錯(cuò):ModuleNotFoundError: No module named ‘pymssql‘的解決方法,如果有遇到相同問題的朋友可以參考閱讀一下本文2023-10-10
如何利用python在剪貼板上讀取/寫入數(shù)據(jù)
說起處理數(shù)據(jù)就離不開導(dǎo)入導(dǎo)出,而我們使用Pandas時(shí)候最常用的就是read_excel、read_csv了,下面這篇文章主要給大家介紹了關(guān)于如何利用python在剪貼板上讀取/寫入數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-07-07

