This is the third day of my participation in Gwen Challenge

Author: Cola

Source: Coke’s path to data analysis

Please contact authorization for reprinting (wechat ID: data_COLA)

Writing in the front

Hello, everyone, I’m Coke.

A Series is a one-dimensional array consisting of a set of data and a set of row indexes. You can think of it as a column of data in Excel without a column name.

To use Series, import the PANDAS module.

import pandas as pd
Copy the code

1. To build

Create a new Series, using pd.Series, which can be created from the list

In [1]:S1 = pd.series ([2,4,6,8]) S1 Out[1]:0 2 1 4 2 63 8 dtype:int63Copy the code

The first column of the output [0,1,2,3] is the index of the sequence S1, and the second column [2,4,6,8] is the value of S1. The most subjective difference from lists is that lists are arranged horizontally, whereas sequences are arranged vertically.

As you can see, the default index increments from 0 without specifying an index. You can also specify an index, as shown in the following example: S2 is given an index incrementing from 1, and assigned with index.

In [2] : S2 = pd Series ([" a ", "b", "d", "e"), the index = [1, 2, 3, 4]) S2 Out [2] : 1 a, 2 b 3 d, 4 e dtype: objectCopy the code

It can also be created from a dictionary, where the key is the index of the sequence.

In [3]:S3 = pd.Series({"Tom":101,"Tony":102,"Judy":103})
       S3
Out[3]:Tom  101
       Tony 102
       Judy 103
       dtype:int64
Copy the code

2. To add

Adding data to a sequence actually creates a new sequence and then vertically merges the two sequences. For example, on the basis of S2, S1 is appended to the append method, so as to indirectly realize the addition of data to S2 sequence.

In [4]:S2.append(S1)
Out[4]:1 a
       2 b
       3 d
       4 e
       0 2
       1 4
       2 6
       3 8
       dtype:object
Copy the code

Or maybe I just want to add an “F” to S2, again.

In [5]:S_f = pd.Series(["f"],index = [5])
       S2.append(S_f)
Out[5]:1 a
       2 b
       3 d
       4 e
       5 f
       dtype:object
Copy the code

3. Delete

To drop a, enter the index 1 corresponding to a in the drop column. Note that this is not the default index. It is set to increment from 1.

In [6]:# delete s2. drop([1]) Out[6]:2 b 3 d 4 e 5 f dtype:objectCopy the code

The above example deletes one value, or you can delete multiple values

Drop ([1,3]) Out[7]:2 b 4 e 5 f dtype:objectCopy the code

4. Check

To find out if a value in a sequence also uses the IN operator, I won’t go into that here, but the other method, isin, returns a Boolean value.

Isin ([a,f]) Out[8]:2 FALSE 4 FALSE 5 TRUE DTYPE :boolCopy the code

Find if S2 contains values for “a” and “f”, index 5 is f, so return TRUE, otherwise return FALSE.

To find a value at a specified position in a sequence by accessing an index.

In [9] : # S4 index = pd. The Series ([" c ", "o", "l", "a"], index = [" a ", "2", "three", "four"]) S4 Out [9], "three" lCopy the code

Here we create a new S4 sequence with a custom index [” one “,” two “,” three “,” four “] and find the corresponding value “L” by searching for index “three”.

Slice, access multiple locations, access the top three (including third) data.

In [10]:# slice S4[:3] Out[10]: one c two o three L dType :objectCopy the code

5. change

As with any other data structure, it looks up the index and then assigns a value, such as changing C from S4 to D:

In [11]:# modify S4["一"] = "d" S4 Out[11]: d 2 o 3 l 4 a dtype:objectCopy the code