Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Pandas is a Python library based on NumPy. It provides a wide range of functions and methods for manipulating and manipulating datasets.

Pandas The most widely used data structures are Series (one-dimensional data structures) and DataFrame (two-dimensional data structures).

  • Series is a one-dimensional labeled data structure consisting of a set of data values and a set of labels. There is a one-to-one correspondence between labels and data values. Labels are integers increasing from 0 by default. It can store a variety of data types, including numbers, strings, Python objects, and so on.
  • A DataFrame is a two-dimensional labeled data structure with both index and columns.

After you have learned the basics of Pandas, learn the following questions.

What is the syntax for creating a Series object?

(Difficulty: Super Easy)

Pandas uses the Series() function to create a Series object that calls methods and properties for processing data. code

import pandas as pd

s = pd.Series(data, index)
Copy the code

The parameter data is the input data, which can be constants, lists, ndarray array, etc. Index is an index. The value must be unique. The default value is an integer incrementing from 0.

How to create a Series object from a list, dictionary, or ndarray object?

(Difficulty: Easy)

Analysis: When using Series(), specify the data parameter and the corresponding index parameter. The usage and explanation are shown below. The specific implementation

  • Create a Series object from a list:
data = ["a"."b"."c"."d"]
s1 = pd.Series(data)
s1
Copy the code

The following output is displayed:

0    a
1    b
2    c
3    d
dtype: object
Copy the code

The preceding 0,1,2, and 3 are the indexes of the current Series.

  • Create a Series object from a dictionary:

The dictionary is used as input data. If no index is passed in, the dictionary key will be used to construct the index. When you pass an index, you need to match the index label to the value in the dictionary. The following is an example:

data = {'a' : 0.'b' : 1.'c' : 2}
s2 = pd.Series(data)
s2
Copy the code

The output is:

a    0
b    1
c    2
dtype: int64
Copy the code

When passing an index:

data = {'a' : 0.'b' : 1.'c' : 2}
s3 = pd.Series(data, index=['c'.'a'.'b'.'d'])
s3
Copy the code

The output is:

C 2.0 a 0.0 b 1.0 D NaN DType: float64Copy the code

NaN is used to populate when the index value passed cannot be found.

  • Create a Series object from NDARray

Ndarray is an array type in NumPy. When data is NDARry, the index passed must have the same length as the array. Here we will create an NDARray array and then create a Series object from NDARray:

import numpy as np

data = np.array(["a"."b"."c"."d"])
s4 = pd.Series(data)
s4
Copy the code

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!