Python数据处理之pd.Series()函数的基本使用

2022-07-14,,,,

1.series介绍

pandas模块的数据结构主要有两种:1.series 2.dataframe

series 是一维数组,基于numpy的ndarray 结构

series([data, index, dtype, name, copy, …])    
# one-dimensional ndarray with axis labels (including time series).

2.series创建

import pandas as pd 
import numpy as np

1.pd.series([list],index=[list])

参数为list ,index为可选参数,若不填写则默认为index从0开始

obj = pd.series([4, 7, -5, 3, 7, np.nan])
obj

输出结果为:

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

输出结果为:

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

输出结果为:

a    10
b    20
c    30
d    40
e    50
dtype: int64

可以通过dataframe中某一行或者某一列创建序列

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 计算、描述性统计

 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()

输出结果为:

 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()

输出结果为:

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')

输出结果为:

bob      3.0
steve    4.0
jeff     1.0
ryan     2.0
jeff     4.0
ryan     nan
dtype: float64

总结

到此这篇关于python数据处理之pd.series()函数的基本使用的文章就介绍到这了,更多相关python pd.series()函数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《Python数据处理之pd.Series()函数的基本使用.doc》

下载本文的Word格式文档,以方便收藏与打印。