Python數(shù)據(jù)處理之pd.Series()函數(shù)的基本使用
1.Series介紹
Pandas模塊的數(shù)據(jù)結(jié)構(gòu)主要有兩種:1.Series 2.DataFrame
Series 是一維數(shù)組,基于Numpy的ndarray 結(jié)構(gòu)
Series([data, index, dtype, name, copy, …]) # One-dimensional ndarray with axis labels (including time series).
2.Series創(chuàng)建
import Pandas as pd import numpy as np
1.pd.Series([list],index=[list])
參數(shù)為list ,index為可選參數(shù),若不填寫(xiě)則默認(rèn)為index從0開(kāi)始
obj = pd.Series([4, 7, -5, 3, 7, np.nan]) obj
輸出結(jié)果為:
0 4.0
1 7.0
2 -5.0
3 3.0
4 7.0
5 NaN
dtype: float64
2.pd.Series(np.arange())
arr = np.arange(6) s = pd.Series(arr) s
輸出結(jié)果為:
0 0
1 1
2 2
3 3
4 4
5 5
dtype: int32
pd.Series({dict})
d = {'a':10,'b':20,'c':30,'d':40,'e':50}
s = pd.Series(d)
s
輸出結(jié)果為:
a 10
b 20
c 30
d 40
e 50
dtype: int64
可以通過(guò)DataFrame中某一行或者某一列創(chuàng)建序列
3 Series基本屬性
- Series.values:Return Series as ndarray or ndarray-like depending on the dtype
obj.values # array([ 4., 7., -5., 3., 7., nan])
- Series.index:The index (axis labels) of the Series.
obj.index # RangeIndex(start=0, stop=6, step=1)
- Series.name:Return name of the Series.
4 索引
- Series.loc:Access a group of rows and columns by label(s) or a boolean array.
- Series.iloc:Purely integer-location based indexing for selection by position.
5 計(jì)算、描述性統(tǒng)計(jì)
Series.value_counts:Return a Series containing counts of unique values.
index = ['Bob', 'Steve', 'Jeff', 'Ryan', 'Jeff', 'Ryan'] obj = pd.Series([4, 7, -5, 3, 7, np.nan],index = index) obj.value_counts()
輸出結(jié)果為:
7.0 2
3.0 1
-5.0 1
4.0 1
dtype: int64
6 排序
Series.sort_values
Series.sort_values(self, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
Parameters:
| Parameters | Description |
|---|---|
| axis | {0 or ‘index’}, default 0,Axis to direct sorting. The value ‘index’ is accepted for compatibility with DataFrame.sort_values. |
| ascendin | bool, default True,If True, sort values in ascending order, otherwise descending. |
| inplace | bool, default FalseIf True, perform operation in-place. |
| kind | {‘quicksort’, ‘mergesort’ or ‘heapsort’}, default ‘quicksort’Choice of sorting algorithm. See also numpy.sort() for more information. ‘mergesort’ is the only stable algorithm. |
| na_position | {‘first’ or ‘last’}, default ‘last’,Argument ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end. |
Returns:
Series:Series ordered by values.
obj.sort_values()
輸出結(jié)果為:
Jeff -5.0
Ryan 3.0
Bob 4.0
Steve 7.0
Jeff 7.0
Ryan NaN
dtype: float64
- Series.rank
Series.rank(self, axis=0, method='average', numeric_only=None, na_option='keep', ascending=True, pct=False)[source]
Parameters:
| Parameters | Description |
|---|---|
| axis | {0 or ‘index’, 1 or ‘columns’}, default 0Index to direct ranking. |
| method | {‘average’, ‘min’, ‘max’, ‘first’, ‘dense’}, default ‘average’How to rank the group of records that have the same value (i.e. ties): average, average rank of the group; min: lowest rank in the group; max: highest rank in the group; first: ranks assigned in order they appear in the array; dense: like ‘min’, but rank always increases by 1,between groups |
| numeric_only | bool, optional,For DataFrame objects, rank only numeric columns if set to True. |
| na_option | {‘keep’, ‘top’, ‘bottom’}, default ‘keep’, How to rank NaN values:;keep: assign NaN rank to NaN values; top: assign smallest rank to NaN values if ascending; bottom: assign highest rank to NaN values if ascending |
| ascending | bool, default True Whether or not the elements should be ranked in ascending order. |
| pct | bool, default False Whether or not to display the returned rankings in percentile form. |
Returns:
same type as caller :Return a Series or DataFrame with data ranks as values.
# obj.rank() #從大到小排,NaN還是NaN obj.rank(method='dense') # obj.rank(method='min') # obj.rank(method='max') # obj.rank(method='first') # obj.rank(method='dense')
輸出結(jié)果為:
Bob 3.0
Steve 4.0
Jeff 1.0
Ryan 2.0
Jeff 4.0
Ryan NaN
dtype: float64
總結(jié)
到此這篇關(guān)于Python數(shù)據(jù)處理之pd.Series()函數(shù)的基本使用的文章就介紹到這了,更多相關(guān)Python pd.Series()函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python supervisor強(qiáng)大的進(jìn)程管理工具的使用
這篇文章主要介紹了Python supervisor強(qiáng)大的進(jìn)程管理工具的使用,本文主要跟大家分享在類unix操作系統(tǒng)下supervisor的使用以及一些關(guān)于進(jìn)程的知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Python 余弦相似度與皮爾遜相關(guān)系數(shù) 計(jì)算實(shí)例
今天小編就為大家分享一篇Python 余弦相似度與皮爾遜相關(guān)系數(shù) 計(jì)算實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
python之wxPython應(yīng)用實(shí)例
這篇文章主要介紹了python之wxPython應(yīng)用實(shí)例,以加載圖片為例講述了wxPython的用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-09-09
python3實(shí)現(xiàn)單目標(biāo)粒子群算法
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)單目標(biāo)粒子群算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
python中將zip壓縮包轉(zhuǎn)為gz.tar的方法
今天小編就為大家分享一篇python中將zip壓縮包轉(zhuǎn)為gz.tar的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
利用python+request通過(guò)接口實(shí)現(xiàn)人員通行記錄上傳功能
這篇文章主要介紹了利用python+request通過(guò)接口實(shí)現(xiàn)人員通行記錄上傳功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Pytorch出現(xiàn)錯(cuò)誤Attribute?Error:module?‘torch‘?has?no?attrib
這篇文章主要給大家介紹了關(guān)于Pytorch出現(xiàn)錯(cuò)誤Attribute?Error:module?‘torch‘?has?no?attribute?'_six'解決的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
動(dòng)感網(wǎng)頁(yè)相冊(cè) python編寫(xiě)簡(jiǎn)單文件夾內(nèi)圖片瀏覽工具
這篇文章主要為大家詳細(xì)介紹了動(dòng)感網(wǎng)頁(yè)相冊(cè)的制作方法,即利用python編寫(xiě)簡(jiǎn)單文件夾內(nèi)圖片瀏覽工具,感興趣的小伙伴們可以參考一下2016-08-08
基于Python和Tkinter實(shí)現(xiàn)高考倒計(jì)時(shí)功能
隨著高考的臨近,每個(gè)考生都在緊鑼密鼓地復(fù)習(xí),這時(shí)候,一款實(shí)用的倒計(jì)時(shí)軟件能有效幫助你規(guī)劃剩余時(shí)間,提醒你不要浪費(fèi)每一分每一秒,今天,我們來(lái)聊聊一款基于Python和Tkinter開(kāi)發(fā)的高考倒計(jì)時(shí)軟件,功能簡(jiǎn)單卻極具實(shí)用性,讓你在緊張的備考過(guò)程中不再迷失2025-03-03

