In-depth understanding of Pandas in python (code examples)

不言
Release: 2018-08-30 10:20:08
Original
2186 people have browsed it

This article brings you an in-depth understanding of Pandas in python (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Filter

First create a 6X4 matrix data.

dates = pd.date_range('20180830', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D'])
print(df)
Copy after login

Print:

             A   B   C   D
2018-08-30   0   1   2   3
2018-08-31   4   5   6   7
2018-09-01   8   9  10  11
2018-09-02  12  13  14  15
2018-09-03  16  17  18  19
2018-09-04  20  21  22  23
Copy after login

Simple filtering

If we want to select the data in DataFrame, two ways are described below, they can To achieve the same goal:

print(df['A'])
print(df.A)

"""
2018-08-30     0
2018-08-31     4
2018-09-01     8
2018-09-02    12
2018-09-03    16
2018-09-04    20
Freq: D, Name: A, dtype: int64
"""
Copy after login

To make the selection span multiple rows or columns:

print(df[0:3])
 
"""
            A  B   C   D
2018-08-30  0  1   2   3
2018-08-31  4  5   6   7
2018-09-01  8  9  10  11
"""

print(df['20180830':'20180901'])

"""
            A  B   C   D
2018-08-30  0  1   2   3
2018-08-31  4  5   6   7
2018-09-01  8  9  10  11
"""
Copy after login

If df[3:3] will be an empty object. The latter selects data between the tags 20180830 to 20180901, and includes these two tags .

You can also select via loc, iloc, ix.

Related recommendations:

A brief introduction to using the Pandas library to process big data in Python

Through pandas in Python Detailed explanation of the library's analysis of cdn logs

The above is the detailed content of In-depth understanding of Pandas in python (code examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!