Hướng dẫn how do you slice a column in python? - làm thế nào để bạn cắt một cột trong python?

Tổng quan

Giảng dạy: 30 phút Bài tập: 30 phút 30 min
Exercises: 30 min

Câu hỏi

  • Làm thế nào tôi có thể truy cập dữ liệu cụ thể trong tập dữ liệu của tôi?

  • Làm thế nào Python và Pandas có thể giúp tôi phân tích dữ liệu của mình?

Mục tiêu

  • Mô tả lập chỉ mục dựa trên 0 là gì.

  • Thao tác và trích xuất dữ liệu bằng các tiêu đề cột và vị trí chỉ mục.

  • Sử dụng cắt để chọn các bộ dữ liệu từ DataFrame.

  • Sử dụng chỉ mục dựa trên nhãn và số nguyên để chọn phạm vi dữ liệu trong DataFrame.

  • Chỉ định các giá trị trong tập hợp con của DataFrame.

  • Tạo một bản sao của DataFrame.

  • Truy vấn / Chọn một tập hợp dữ liệu bằng cách sử dụng một bộ tiêu chí bằng cách sử dụng các toán tử sau:

    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    6,
    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    7,
    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    8,
    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    9,
    # Select the species and plot columns from the DataFrame
    surveys_df[['species_id', 'plot_id']]
    
    # What happens when you flip the order?
    surveys_df[['plot_id', 'species_id']]
    
    # What happens if you ask for a column that doesn't exist?
    surveys_df['speciess']
    
    0,
    # Select the species and plot columns from the DataFrame
    surveys_df[['species_id', 'plot_id']]
    
    # What happens when you flip the order?
    surveys_df[['plot_id', 'species_id']]
    
    # What happens if you ask for a column that doesn't exist?
    surveys_df['speciess']
    
    1.

  • Xác định vị trí tập hợp dữ liệu bằng mặt nạ.

  • Mô tả các đối tượng Boolean trong Python và thao tác dữ liệu bằng Booleans.

Trong tập đầu tiên của bài học này, chúng tôi đã đọc một tệp CSV vào khung dữ liệu gấu trúc. Chúng tôi đã học cách:

  • Lưu một khung dữ liệu vào một đối tượng được đặt tên,
  • thực hiện toán học cơ bản trên dữ liệu,
  • tính toán số liệu thống kê tóm tắt và
  • Tạo sơ đồ dựa trên dữ liệu chúng tôi tải vào gấu trúc.

Trong bài học này, chúng tôi sẽ khám phá các cách để truy cập các phần khác nhau của dữ liệu bằng cách sử dụng:

  • indexing,
  • cắt lát, và
  • subsetting.

Tải dữ liệu của chúng tôi

Chúng tôi sẽ tiếp tục sử dụng bộ dữ liệu khảo sát mà chúng tôi đã làm việc trong tập trước. Hãy để mở lại và đọc lại trong dữ liệu:

# Make sure pandas is loaded
import pandas as pd

# Read in the survey CSV
surveys_df = pd.read_csv("data/surveys.csv")

Lập chỉ mục và cắt trong Python

Chúng tôi thường muốn làm việc với các tập hợp con của đối tượng DataFrame. Có nhiều cách khác nhau để thực hiện điều này bao gồm: sử dụng nhãn (tiêu đề cột), phạm vi số hoặc vị trí chỉ mục X, Y cụ thể.DataFrame object. There are different ways to accomplish this including: using labels (column headings), numeric ranges, or specific x,y index locations.

Chọn dữ liệu bằng cách sử dụng nhãn (tiêu đề cột)

Chúng tôi sử dụng dấu ngoặc vuông

# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
2 để chọn một tập hợp con của đối tượng Python. Ví dụ: chúng ta có thể chọn tất cả dữ liệu từ một cột có tên
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
3 từ
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
4 DataFrame theo tên. Có hai cách để làm điều này:

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id

Chúng ta cũng có thể tạo một đối tượng mới chỉ chứa dữ liệu trong cột

# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
3 như sau:

# Creates an object, surveys_species, that only contains the `species_id` column
surveys_species = surveys_df['species_id']

Chúng ta cũng có thể vượt qua danh sách các tên cột, làm chỉ mục để chọn các cột theo thứ tự đó. Điều này rất hữu ích khi chúng ta cần tổ chức lại dữ liệu của mình.

Lưu ý: Nếu một tên cột không chứa trong DataFrame, một ngoại lệ (lỗi) sẽ được nêu ra. If a column name is not contained in the DataFrame, an exception (error) will be raised.

# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']

Python cho chúng ta biết loại lỗi nào trong dấu vết, ở phía dưới, nó nói

# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
6 có nghĩa là
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
7 không phải là tên cột hợp lệ (cũng không phải là khóa hợp lệ trong từ điển loại dữ liệu Python liên quan).

Lời nhắc nhở

Ngôn ngữ Python và các mô -đun của nó (như gấu trúc) định nghĩa các từ dành riêng không nên được sử dụng làm định danh khi gán các đối tượng và tên biến. Ví dụ về các từ dành riêng trong Python bao gồm các giá trị boolean

# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
8 và
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
9, toán tử
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
0,
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
1 và
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
2, trong số những người khác. Danh sách đầy đủ các từ dành riêng cho phiên bản Python 3 được cung cấp tại https://docs.python.org/3/reference/lexical_analysis.html#identifiers.

Khi đặt tên cho các đối tượng và biến, nó cũng rất quan trọng để tránh sử dụng tên của các cấu trúc và phương thức dữ liệu tích hợp. Ví dụ, một danh sách là một loại dữ liệu tích hợp. Có thể sử dụng từ ‘Danh sách, làm định danh cho một đối tượng mới, ví dụ

# Create a list of numbers:
a = [1, 2, 3, 4, 5]
3. Tuy nhiên, sau đó bạn sẽ không thể tạo một danh sách trống bằng cách sử dụng
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
4 hoặc chuyển đổi một tuple thành danh sách bằng cách sử dụng
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
5.

Lời nhắc nhở

Ngôn ngữ Python và các mô -đun của nó (như gấu trúc) định nghĩa các từ dành riêng không nên được sử dụng làm định danh khi gán các đối tượng và tên biến. Ví dụ về các từ dành riêng trong Python bao gồm các giá trị boolean

# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
8 và
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
9, toán tử
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
0,
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
1 và
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
2, trong số những người khác. Danh sách đầy đủ các từ dành riêng cho phiên bản Python 3 được cung cấp tại https://docs.python.org/3/reference/lexical_analysis.html#identifiers.

Khi đặt tên cho các đối tượng và biến, nó cũng rất quan trọng để tránh sử dụng tên của các cấu trúc và phương thức dữ liệu tích hợp. Ví dụ, một danh sách là một loại dữ liệu tích hợp. Có thể sử dụng từ ‘Danh sách, làm định danh cho một đối tượng mới, ví dụ

# Create a list of numbers:
a = [1, 2, 3, 4, 5]
3. Tuy nhiên, sau đó bạn sẽ không thể tạo một danh sách trống bằng cách sử dụng
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
4 hoặc chuyển đổi một tuple thành danh sách bằng cách sử dụng
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
5.

# Create a list of numbers:
a = [1, 2, 3, 4, 5]

Hướng dẫn how do you slice a column in python? - làm thế nào để bạn cắt một cột trong python?
Hướng dẫn how do you slice a column in python? - làm thế nào để bạn cắt một cột trong python?

  1. Python sử dụng lập chỉ mục dựa trên 0.

  2. Hãy để bản thân nhắc nhở bản thân rằng Python sử dụng lập chỉ mục dựa trên 0. Điều này có nghĩa là phần tử đầu tiên trong một đối tượng được đặt tại vị trí

    # Create a list of numbers:
    a = [1, 2, 3, 4, 5]
    
    6. Điều này khác với các công cụ khác như R và MATLAB rằng các phần tử chỉ mục trong các đối tượng bắt đầu từ 1.

  3. Mã dưới đây trả về giá trị nào?

  4. Còn cái này thì sao:

Trong ví dụ trên, gọi # Create a list of numbers: a = [1, 2, 3, 4, 5] 7 trả về lỗi. Tại sao vậy?

Thế còn?

# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]

Cắt các tập hợp con của các hàng trong Python

# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]

Cắt bằng cách sử dụng toán tử

# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
2 chọn một tập hợp các hàng và/hoặc cột từ DataFrame. Để cắt ra một tập hợp các hàng, bạn sử dụng cú pháp sau:
# Create a list of numbers:
a = [1, 2, 3, 4, 5]
9. Khi cắt trong gấu trúc, giới hạn bắt đầu được bao gồm trong đầu ra. Giới hạn dừng là một bước ngoài hàng bạn muốn chọn. Vì vậy, nếu bạn muốn chọn hàng 0, 1 và 2 mã của bạn sẽ trông như thế này:

Dừng bị ràng buộc trong Python khác với những gì bạn có thể quen với các ngôn ngữ như Matlab và R.

Chúng tôi cũng có thể chỉ định lại các giá trị trong các tập hợp con của DataFrame.

Nhưng trước khi chúng ta làm điều đó, hãy để Lôi nhìn vào sự khác biệt giữa khái niệm sao chép các đối tượng và khái niệm tham chiếu các đối tượng trong Python.

# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df

Sao chép các đối tượng so với các đối tượng tham chiếu trong Pythonnot create a copy of our DataFrame. Instead,

# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
3 creates a new variable
# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
5 that references the same object that
# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
6 refers to. To state this another way, there is only one object (the DataFrame), and both
# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
6 and
# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
5 refer to it.

Ngược lại, phương thức

# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
9 cho DataFrame tạo ra một bản sao thực sự của DataFrame.

Hãy cùng xem xét những gì xảy ra khi chúng ta gán lại các giá trị trong một tập hợp con của DataFrame tham chiếu một đối tượng DataFrame khác:

# Assign the value `0` to the first three rows of data in the DataFrame
ref_surveys_df[0:3] = 0

Hãy để thử mã sau:

# ref_surveys_df was created using the '=' operator
ref_surveys_df.head()

# surveys_df is the original dataframe
surveys_df.head()

Sự khác biệt giữa hai khung dữ liệu này là gì?

Khi chúng tôi chỉ định 3 cột đầu tiên, giá trị của

# Create a list of numbers:
a = [1, 2, 3, 4, 5]
6 bằng cách sử dụng DataFrame
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
1, DataFrame
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
4 cũng được sửa đổi. Hãy nhớ rằng chúng tôi đã tạo đối tượng tham chiếu
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
1 ở trên khi chúng tôi đã làm
# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
0. Hãy nhớ
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
4 và
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
1 tham khảo cùng một đối tượng DataFrame chính xác. Nếu một trong hai thay đổi đối tượng, thì lại sẽ thấy các thay đổi tương tự đối với đối tượng tham chiếu.

Để xem xét và tóm tắt lại::

  • Sao chép sử dụng phương thức DataFrame từ

    # Select rows 0, 1, 2 (row 3 is not selected)
    surveys_df[0:3]
    
    9 uses the dataframe’s
    # Select rows 0, 1, 2 (row 3 is not selected)
    surveys_df[0:3]
    
    9 method

    # TIP: use the .head() method we saw earlier to make output shorter
    # Method 1: select a 'subset' of the data using the column name
    surveys_df['species_id']
    
    # Method 2: use the column name as an 'attribute'; gives the same output
    surveys_df.species_id
    
    0

  • Một tham chiếu được tạo bằng toán tử

    # Select rows 0, 1, 2 (row 3 is not selected)
    surveys_df[0:3]
    
    2Reference is created using the
    # Select rows 0, 1, 2 (row 3 is not selected)
    surveys_df[0:3]
    
    2 operator

    # TIP: use the .head() method we saw earlier to make output shorter
    # Method 1: select a 'subset' of the data using the column name
    surveys_df['species_id']
    
    # Method 2: use the column name as an 'attribute'; gives the same output
    surveys_df.species_id
    
    1

Được rồi, điều đó đủ về điều đó. Hãy để tạo ra một khung dữ liệu sạch hoàn toàn mới từ tệp CSV dữ liệu gốc.

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
2

Cắt các tập hợp con của các hàng và cột trong Python

Chúng tôi có thể chọn các phạm vi cụ thể của dữ liệu của chúng tôi theo cả hai hướng hàng và cột bằng cách sử dụng lập chỉ mục dựa trên nhãn hoặc số nguyên.

  • # Select the first 5 rows (rows 0, 1, 2, 3, 4)
    surveys_df[:5]
    
    # Select the last element in the list
    # (the slice starts at the last element, and ends at the end of the list)
    surveys_df[-1:]
    
    9 chủ yếu là lập chỉ mục dựa trên nhãn. Số nguyên có thể được sử dụng nhưng chúng được hiểu là một nhãn.
  • # Using the 'copy() method'
    true_copy_surveys_df = surveys_df.copy()
    
    # Using the '=' operator
    ref_surveys_df = surveys_df
    
    0 chủ yếu là chỉ số dựa trên số nguyên

Để chọn một tập hợp con của các hàng và cột từ DataFrame của chúng tôi, chúng tôi có thể sử dụng phương thức

# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
0. Ví dụ: chúng ta có thể chọn tháng, ngày và năm (cột 2, 3 và 4 nếu chúng ta bắt đầu đếm ở mức 1), như thế này:and columns from our DataFrame, we can use the
# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
0 method. For example, we can select month, day and year (columns 2, 3 and 4 if we start counting at 1), like this:

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
3

cung cấp đầu raoutput

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
4

Lưu ý rằng chúng tôi đã yêu cầu một lát từ 0: 3. Điều này mang lại 3 hàng dữ liệu. Khi bạn yêu cầu 0: 3, bạn thực sự đang nói với Python bắt đầu tại INDEX 0 và chọn các hàng 0, 1, 2 lên đến nhưng không bao gồm 3.up to but not including 3.

Hãy cùng khám phá một số cách khác để lập chỉ mục và chọn các tập hợp con của dữ liệu:

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
5

Lưu ý: Các nhãn phải được tìm thấy trong DataFrame hoặc bạn sẽ nhận được

# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
2.: Labels must be found in the DataFrame or you will get a
# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
2.

Lập chỉ mục bởi các nhãn

# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
9 khác với việc lập chỉ mục bởi các số nguyên
# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
0. Với
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
9, cả ràng buộc bắt đầu và giới hạn dừng đều bao gồm. Khi sử dụng
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
9, các số nguyên có thể được sử dụng, nhưng các số nguyên đề cập đến nhãn chỉ mục chứ không phải vị trí. Ví dụ: sử dụng
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
9 và chọn 1: 4 sẽ nhận được kết quả khác với việc sử dụng
# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
0 để chọn hàng 1: 4.inclusive. When using
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
9, integers can be used, but the integers refer to the index label and not the position. For example, using
# Select the first 5 rows (rows 0, 1, 2, 3, 4)
surveys_df[:5]

# Select the last element in the list
# (the slice starts at the last element, and ends at the end of the list)
surveys_df[-1:]
9 and select 1:4 will get a different result than using
# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
0 to select rows 1:4.

Chúng tôi cũng có thể chọn một giá trị dữ liệu cụ thể bằng cách sử dụng vị trí hàng và cột trong DataFrame và

# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
0 lập chỉ mục:

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
6

Trong ví dụ

# Using the 'copy() method'
true_copy_surveys_df = surveys_df.copy()

# Using the '=' operator
ref_surveys_df = surveys_df
0 này,

cho đầu raoutput

Hãy nhớ rằng việc lập chỉ mục Python bắt đầu ở 0. Vì vậy, vị trí chỉ mục [2, 6] chọn phần tử là 3 hàng xuống và 7 cột trong khung dữ liệu.

Thử thách - Phạm vi

  1. Điều gì xảy ra khi bạn thực hiện:

    • # Assign the value `0` to the first three rows of data in the DataFrame
      ref_surveys_df[0:3] = 0
      
      1
    • # Assign the value `0` to the first three rows of data in the DataFrame
      ref_surveys_df[0:3] = 0
      
      2
    • # Assign the value `0` to the first three rows of data in the DataFrame
      ref_surveys_df[0:3] = 0
      
      3
  2. Điều gì xảy ra khi bạn gọi:

    • # Assign the value `0` to the first three rows of data in the DataFrame
      ref_surveys_df[0:3] = 0
      
      4
    • # Assign the value `0` to the first three rows of data in the DataFrame
      ref_surveys_df[0:3] = 0
      
      5
  • Hai lệnh khác nhau như thế nào?

Tập hợp dữ liệu bằng cách sử dụng các tiêu chí

Chúng tôi cũng có thể chọn một tập hợp dữ liệu của chúng tôi bằng các tiêu chí. Ví dụ: chúng ta có thể chọn tất cả các hàng có giá trị năm 2002:

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
7

Trong đó tạo ra đầu ra sau:

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
8

Hoặc chúng ta có thể chọn tất cả các hàng không chứa năm 2002:

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
9

Chúng ta cũng có thể xác định các bộ tiêu chí:

# Creates an object, surveys_species, that only contains the `species_id` column
surveys_species = surveys_df['species_id']
0

Tờ Cheat Python cú pháp

Chúng ta có thể sử dụng cú pháp dưới đây khi truy vấn dữ liệu theo tiêu chí từ DataFrame. Thử nghiệm chọn các tập hợp con khác nhau của dữ liệu khảo sát trên mạng.

  • Bằng:
    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    6
  • Không bằng:
    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    7
  • Lớn hơn, nhỏ hơn:
    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    8 hoặc
    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    9
  • Lớn hơn hoặc bằng
    # Select the species and plot columns from the DataFrame
    surveys_df[['species_id', 'plot_id']]
    
    # What happens when you flip the order?
    surveys_df[['plot_id', 'species_id']]
    
    # What happens if you ask for a column that doesn't exist?
    surveys_df['speciess']
    
    0
  • Nhỏ hơn hoặc bằng
    # Select the species and plot columns from the DataFrame
    surveys_df[['species_id', 'plot_id']]
    
    # What happens when you flip the order?
    surveys_df[['plot_id', 'species_id']]
    
    # What happens if you ask for a column that doesn't exist?
    surveys_df['speciess']
    
    1

Thử thách - Truy vấn

  1. Chọn một tập hợp con của các hàng trong khung dữ liệu

    # Select the species and plot columns from the DataFrame
    surveys_df[['species_id', 'plot_id']]
    
    # What happens when you flip the order?
    surveys_df[['plot_id', 'species_id']]
    
    # What happens if you ask for a column that doesn't exist?
    surveys_df['speciess']
    
    4 có chứa dữ liệu từ năm 1999 và chứa các giá trị trọng lượng nhỏ hơn hoặc bằng 8. Bạn đã kết thúc bao nhiêu hàng? Hàng xóm của bạn đã nhận được gì?

  2. Bạn có thể sử dụng lệnh

    # ref_surveys_df was created using the '=' operator
    ref_surveys_df.head()
    
    # surveys_df is the original dataframe
    surveys_df.head()
    
    3 trong Python để truy vấn DataFrame dựa trên danh sách các giá trị như sau:

    # Creates an object, surveys_species, that only contains the `species_id` column
    surveys_species = surveys_df['species_id']
    
    1

Sử dụng chức năng

# ref_surveys_df was created using the '=' operator
ref_surveys_df.head()

# surveys_df is the original dataframe
surveys_df.head()
3 để tìm tất cả các lô có chứa các loài cụ thể trong các cuộc khảo sát dữ liệu. Có bao nhiêu bản ghi chứa các giá trị này?

  1. Thử nghiệm với các truy vấn khác. Tạo một truy vấn tìm thấy tất cả các hàng có giá trị trọng lượng> hoặc bằng 0.

  2. Biểu tượng

    # ref_surveys_df was created using the '=' operator
    ref_surveys_df.head()
    
    # surveys_df is the original dataframe
    surveys_df.head()
    
    5 trong Python có thể được sử dụng để trả về đối diện với lựa chọn mà bạn chỉ định trong Python. Nó tương đương với không phải là vào. Viết một truy vấn chọn tất cả các hàng có giới tính không bằng ‘M, hoặc‘ F, trong dữ liệu của các cuộc khảo sát trên mạng.is not in. Write a query that selects all rows with sex NOT equal to ‘M’ or ‘F’ in the “surveys” data.

Sử dụng mặt nạ để xác định một điều kiện cụ thể

Một mặt nạ có thể hữu ích để định vị nơi tồn tại một tập hợp các giá trị cụ thể hoặc không tồn tại - ví dụ: NAN hoặc không phải là một số giá trị số. Để hiểu mặt nạ, chúng ta cũng cần hiểu các đối tượng

# ref_surveys_df was created using the '=' operator
ref_surveys_df.head()

# surveys_df is the original dataframe
surveys_df.head()
6 trong Python.mask can be useful to locate where a particular subset of values exist or don’t exist - for example, NaN, or “Not a Number” values. To understand masks, we also need to understand
# ref_surveys_df was created using the '=' operator
ref_surveys_df.head()

# surveys_df is the original dataframe
surveys_df.head()
6 objects in Python.

Các giá trị Boolean bao gồm

# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
8 hoặc
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
9. Ví dụ,

# Creates an object, surveys_species, that only contains the `species_id` column
surveys_species = surveys_df['species_id']
2

Khi chúng tôi hỏi Python xem

# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
6 có lớn hơn 5 hay không, nó sẽ trả về
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
9. Đây là cách của Python, để nói rằng không có. Thật vậy, giá trị của
# Select rows 0, 1, 2 (row 3 is not selected)
surveys_df[0:3]
6 là 5 và 5 không lớn hơn 5.

Để tạo mặt nạ Boolean:

  • Đặt tiêu chí đúng / sai (ví dụ:
    # TIP: use the .head() method we saw earlier to make output shorter
    # Method 1: select a 'subset' of the data using the column name
    surveys_df['species_id']
    
    # Method 2: use the column name as an 'attribute'; gives the same output
    surveys_df.species_id
    
    02)
  • Python sau đó sẽ đánh giá từng giá trị trong đối tượng để xác định xem giá trị có đáp ứng các tiêu chí hay không (đúng) hay không (sai).
  • Python tạo ra một đối tượng đầu ra có hình dạng giống như đối tượng gốc, nhưng với giá trị
    # Select the species and plot columns from the DataFrame
    surveys_df[['species_id', 'plot_id']]
    
    # What happens when you flip the order?
    surveys_df[['plot_id', 'species_id']]
    
    # What happens if you ask for a column that doesn't exist?
    surveys_df['speciess']
    
    8 hoặc
    # Select the species and plot columns from the DataFrame
    surveys_df[['species_id', 'plot_id']]
    
    # What happens when you flip the order?
    surveys_df[['plot_id', 'species_id']]
    
    # What happens if you ask for a column that doesn't exist?
    surveys_df['speciess']
    
    9 cho mỗi vị trí chỉ mục.

Hãy để thử điều này. Hãy để xác định tất cả các vị trí trong dữ liệu khảo sát có giá trị dữ liệu NULL (thiếu hoặc NAN). Chúng ta có thể sử dụng phương pháp

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
05 để làm điều này. Phương pháp
# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
05 sẽ so sánh từng ô với giá trị null. Nếu một phần tử có giá trị null, nó sẽ được gán giá trị
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
8 trong đối tượng đầu ra.

Một đoạn đầu ra dưới đây:

# Creates an object, surveys_species, that only contains the `species_id` column
surveys_species = surveys_df['species_id']
3

Để chọn các hàng có giá trị null, chúng ta có thể sử dụng mặt nạ làm chỉ mục để tập hợp dữ liệu của chúng tôi như sau:

# Creates an object, surveys_species, that only contains the `species_id` column
surveys_species = surveys_df['species_id']
4

Lưu ý rằng cột

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
08 của DataFrame của chúng tôi chứa nhiều giá trị
# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
09 hoặc
# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
10. Chúng tôi sẽ khám phá các cách xử lý vấn đề này trong tập tiếp theo về các kiểu dữ liệu và định dạng.

Chúng ta cũng có thể chạy

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
05 trên một cột cụ thể. Mã dưới đây làm gì?

# Creates an object, surveys_species, that only contains the `species_id` column
surveys_species = surveys_df['species_id']
5

Hãy để một phút để xem xét tuyên bố ở trên. Chúng tôi đang sử dụng đối tượng Boolean

# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
12 làm chỉ mục cho
# Select the species and plot columns from the DataFrame
surveys_df[['species_id', 'plot_id']]

# What happens when you flip the order?
surveys_df[['plot_id', 'species_id']]

# What happens if you ask for a column that doesn't exist?
surveys_df['speciess']
4. Chúng tôi đang yêu cầu Python chọn các hàng có giá trị trọng lượng
# TIP: use the .head() method we saw earlier to make output shorter
# Method 1: select a 'subset' of the data using the column name
surveys_df['species_id']

# Method 2: use the column name as an 'attribute'; gives the same output
surveys_df.species_id
10.

Thử thách - Đặt tất cả lại với nhau

  1. Tạo một khung dữ liệu mới chỉ chứa các quan sát với các giá trị giới tính không phải là nữ hoặc nam. Chỉ định từng giá trị giới tính trong khung dữ liệu mới cho một giá trị mới của ‘X. Xác định số lượng giá trị null trong tập hợp con.not female or male. Assign each sex value in the new DataFrame to a new value of ‘x’. Determine the number of null values in the subset.

  2. Tạo một khung dữ liệu mới chỉ chứa các quan sát của nam hoặc nữ giới tính và trong đó giá trị cân nặng lớn hơn 0. Tạo một biểu đồ thanh xếp chồng lên nhau theo cốt truyện với các giá trị nam so với nữ được xếp chồng lên mỗi ô.

Những điểm chính

  • Trong Python, các phần dữ liệu có thể được truy cập bằng các chỉ số, lát cắt, tiêu đề cột và tập hợp dựa trên điều kiện.

  • Python sử dụng lập chỉ mục dựa trên 0, trong đó phần tử đầu tiên trong danh sách, tuple hoặc bất kỳ cấu trúc dữ liệu nào khác có chỉ số 0.

  • Pandas cho phép các bước khám phá dữ liệu chung như lập chỉ mục dữ liệu, cắt lát và tập hợp có điều kiện.