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類型不止一個會怎么樣? → 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開始,步長為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的追加
- 錯誤寫法: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
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python中的位置參數(shù)和關(guān)鍵字參數(shù)詳解
位置參數(shù)和關(guān)鍵字參數(shù)是 Python 中的兩種不同類型的函數(shù)參數(shù)傳遞方式,位置參數(shù)依賴于參數(shù)的位置順序,而關(guān)鍵字參數(shù)通過參數(shù)名傳遞,不受位置影響,本文通過代碼示例給大家詳細(xì)介紹了python中的位置參數(shù)和關(guān)鍵字參數(shù),需要的朋友可以參考下2023-12-12Python爬蟲入門案例之回車桌面壁紙網(wǎng)美女圖片采集
讀萬卷書不如行萬里路,學(xué)的扎不扎實(shí)要通過實(shí)戰(zhàn)才能看出來,今天小編給大家?guī)硪粋€python爬蟲案例,采集回車桌面網(wǎng)站的美女圖片,大家可以在過程中查缺補(bǔ)漏,看看自己掌握程度怎么樣2021-10-10使用python實(shí)現(xiàn)多維數(shù)據(jù)降維操作
今天小編就為大家分享一篇使用python實(shí)現(xiàn)多維數(shù)據(jù)降維操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Anaconda3+tensorflow2.0.0+PyCharm安裝與環(huán)境搭建(圖文)
這篇文章主要介紹了Anaconda3+tensorflow2.0.0+PyCharm安裝與環(huán)境搭建(圖文),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02