Pandas Series object for Python data analysis

Get into the habit of writing together! This is the 5th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Pandas is a Python data analysis library designed and implemented based on NumPy. Pandas provides a large number of functions and methods that allow us to process data efficiently. It also includes many data processing libraries and some data models, which can be said to be very powerful.

It can be installed with the following command:

conda install pandas
#
pip install pandas
复制代码

Series data structure

There are two most commonly used data structures in Pandas: Series and DataFrame. This article mainly introduces Series and how to create Series objects.

Series is a one-dimensional array consisting of a column of indices and a column of values. The indices and values ​​are in one-to-one correspondence, and can store different types of data types, such as strings, booleans, numbers, Python objects, etc.

Create a Series object

The format for creating a Series object is as follows:

s = pd.Series(data, index)
复制代码

The parameter data is data, which can be a dictionary, list, Numpy ndarray array, etc.; the
parameter index is an index, and the value must be unique, similar to the key of a Python dictionary, which can be omitted. The default is an integer that increases from 0.

  • Create from list:
data = ["a", "b", "c", "d", "e"]
s = pd.Series(data)
s
复制代码
  • Create from dictionary:

When data is a dictionary, if no index is passed in, the index will be constructed according to the keys of the dictionary, and the value corresponding to the index is the value corresponding to the key of the dictionary.

data = {"a": 1, "b": 2, "c": 3}
s = pd.Series(data)
s
复制代码

The resulting output is as follows:

a    1
b    2
c    3
dtype: int64
复制代码
  • Create from an ndarray array:

ndarray is an array type of Numpy. The simple use of Numpy in Python data analysis has been introduced in the previous article.

data = np.array([1, 2, 3, 4])
s = pd.Series(data)
s
复制代码

We can process and analyze the data by calling the corresponding properties and methods through the created Series object, which will be introduced in the next article.

It's not easy to be original. If you guys find it helpful, please give a like and go~

Finally, I would like to thank my girlfriend for her tolerance, understanding and support in work and life!

Guess you like

Origin juejin.im/post/7084925907307069447