Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the pd.Series () function

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the pd.Series () function. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Series introduction

There are two main data structures of Pandas module: 1, Series, 2, DataFrame

Series is an one-dimensional array based on the ndarray structure of NumPy. Pandas will tacitly use 0 to nMel 1 as the index of series, but you can also specify index (you can understand index as key in dict).

2. Series creation

Pd.Series ([list], index= [list])

The parameter list;index is optional. If left empty, the default index starts from 0. If left empty, the length of index should be equal to the length of value.

Import pandas as pd

S=pd.Series ([1, 2, 3, 4, 5], index= ['a girl, girl,

Print s

Pd.Series ({dict})

Take a dictionary structure as a parameter.

Import pandas as pd

S=pd.Series ({'axiaxiahuanglu 1thecontrolling Velcro 2thecontrolling PUBG 3MFFFZ 4JEFING 5})

Print s

3. Value of Series

S [index] or s [[list of index]]

The value operation is similar to an array. When taking multiple values that are not contiguous, you can take list as a parameter.

Import pandas as pd

Import numpy as np

V = np.random.random_sample (50)

S = pd.Series (v)

S1 = s [[3, 13, 23, 33]]

S2 = s [3:13]

S3 = s [43]

Print ("S1", S1)

Print ("S2", S2)

Print (S3, S3)

S1 3 0.064095

13 0.354023

23 0.225739

33 0.959288

Dtype: float64

S2 3 0.064095

4 0.405651

5 0.024181

6 0.367606

7 0.844005

8 0.405313

9 0.102824

10 0.806400

11 0.950502

12 0.735310

Dtype: float64

S3 0.42803253918

4. Series takes the values of head and tail

.head (n); .tail (n)

Take out the first n lines or the last n lines. N is an optional parameter. If you leave it empty, default 5

Import pandas as pd

Import numpy as np

V = np.random.random_sample (50)

S = pd.Series (v)

Print ("s.head ()", s.head ())

Print ("s.head (3)", s.head (3))

Print ("s.tail ()", s.tail ())

Print ("s.head (3)", s.head (3))

S.head () 0 0.714136

1 0.333600

2 0.683784

3 0.044002

4 0.147745

Dtype: float64

S.head (3) 0 0.714136

1 0.333600

2 0.683784

Dtype: float64

S.tail () 45 0.779509

46 0.778341

47 0.331999

48 0.444811

49 0.028520

Dtype: float64

S.head (3) 0 0.714136

1 0.333600

2 0.683784

Dtype: float64

5. Common operations of Series

Import pandas as pd

Import numpy as np

V = [10,3,2,2, np.nan]

V = pd.Series (v)

Print ("len ():", len (v)) # Series length, including NaN

Print ("shape ():", np.shape (v)) # Matrix shape, (,)

Print ("count ():", v.count ()) # Series length, excluding NaN

Print ("unique ():", v.unique ()) # does not repeat values

Print ("value_counts ():\ n", v.value_counts ()) # Statistics the number of times the value appears

Len (): 5 which is a good http://www.wxbhnkyy120.com/ in Wuxi abortion Hospital

Shape (): (5)

Count (): 4

Unique (): [10. 3. 2. Nan]

Value_counts ():

2.0 2

3.0 1

10.0 1

Dtype: int64

6. Series addition

Import pandas as pd

Import numpy as np

V = [10,3,2,2, np.nan]

V = pd.Series (v)

Sum = v [1:3] + v [1:3]

Sum1 = v [1:4] + v [1:4]

Sum2 = v [1:3] + v [1:4]

Sum3 = v [: 3] + v [1:]

Print ("sum", sum)

Print ("sum1", sum1)

Print ("sum2", sum2)

Print ("sum3", sum3)

Sum 1 6.0

2 4.0

Dtype: float64

Sum1 1 6.0

2 4.0

3 4.0

Dtype: float64

Sum2 1 6.0

2 4.0

3 NaN

Dtype: float64

Sum3 0 NaN

1 6.0

2 4.0

3 NaN

4 NaN

Dtype: float64

7. Series search

Range lookup

Import pandas as pd

Import numpy as np

S = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}

Sa = pd.Series (s, name= "age")

Print (SASA > 19)

Jim 22.0

Lj 24.0

Ton 20.0

Name: age, dtype: float64

Median

Import pandas as pd

Import numpy as np

S = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}

Sa = pd.Series (s, name= "age")

Print ("sa.median ()", sa.median ())

Sa.median () 20.0

8. Series assignment

Import pandas as pd

Import numpy as np

S = {"ton": 20, "mary": 18, "jack": 19, "jim": 22, "lj": 24, "car": None}

Sa = pd.Series (s, name= "age")

Print (s)

Print ('-')

Sa ['ton'] = 99

Print (sa)

{'ton': 20,' mary': 18, 'jack': 19,' jim': 22, 'lj': 24,' car': None}

-

Car NaN

Jack 19.0

Jim 22.0

Lj 24.0

Mary 18.0

Ton 99.0

Name: age, dtype: float64

This is the end of the article on "how to use the pd.Series () function". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report