程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python data processing pd Basic use of the series() function

編輯:Python

Catalog

1.Series Introduce

2.Series establish

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

2.pd.Series(np.arange())

3 Series Basic attributes

4 Indexes

5 Calculation 、 Descriptive statistics

6 Sort

summary

1.Series Introduce

Pandas There are two main data structures for modules :1.Series 2.DataFrame

Series It's a one-dimensional array , be based on Numpy Of ndarray structure

Series([data, index, dtype, name, copy, …]) # One-dimensional ndarray with axis labels (including time series).2.Series establish import Pandas as pd import numpy as np1.pd.Series([list],index=[list])

Parameter is list ,index Is an optional parameter , If it is not filled in, the default value is index from 0 Start

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

The output is :

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

The output is :

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

The output is :

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

Can pass DataFrame Create a sequence in a row or column

3 Series Basic attributes

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 Indexes

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 Calculation 、 Descriptive statistics

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

The output is :

 7.0    2
 3.0    1
-5.0    1
 4.0    1
dtype: int64

6 Sort

Series.sort_values

Series.sort_values(self, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')

Parameters:

ParametersDescription axis{0 or ‘index’}, default 0,Axis to direct sorting. The value ‘index’ is accepted for compatibility with DataFrame.sort_values.ascendinbool, default True,If True, sort values in ascending order, otherwise descending.inplacebool, 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()

The output is :

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:

ParametersDescription 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 groupsnumeric_onlybool, 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 ascendingascendingbool, default True Whether or not the elements should be ranked in ascending order.pctbool, 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() # From big to small row ,NaN still NaNobj.rank(method='dense') # obj.rank(method='min')# obj.rank(method='max')# obj.rank(method='first')# obj.rank(method='dense')

The output is :

Bob      3.0
Steve    4.0
Jeff     1.0
Ryan     2.0
Jeff     4.0
Ryan     NaN
dtype: float64

summary

This is about Python Data processing pd.Series() This is the end of the article on the basic use of functions , More about Python pd.Series() Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved