Trong nhiều tình huống “thế giới thực”, dữ liệu mà chúng tôi muốn sử dụng có trong nhiều tệp. Chúng tôi thường cần kết hợp các tệp này thành một DataFrame duy nhất để phân tích dữ liệu. Gói gấu trúc cung cấp nhiều phương pháp khác nhau để kết hợp DataFrames bao gồm
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
4 và # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
5Để làm việc với các ví dụ bên dưới, trước tiên chúng ta cần tải các tệp khảo sát và loài vào pandas DataFrames. Trong iPython
import pandas as pd
surveys_df = pd.read_csv["data/surveys.csv",
keep_default_na=False, na_values=[""]]
surveys_df
record_id month day year plot species sex hindfoot_length weight
0 1 7 16 1977 2 NA M 32 NaN
1 2 7 16 1977 3 NA M 33 NaN
2 3 7 16 1977 2 DM F 37 NaN
3 4 7 16 1977 7 DM M 36 NaN
4 5 7 16 1977 3 DM M 35 NaN
... ... ... ... ... ... ... ... ... ...
35544 35545 12 31 2002 15 AH NaN NaN NaN
35545 35546 12 31 2002 15 AH NaN NaN NaN
35546 35547 12 31 2002 10 RM F 15 14
35547 35548 12 31 2002 7 DO M 36 51
35548 35549 12 31 2002 5 NaN NaN NaN NaN
[35549 rows x 9 columns]
species_df = pd.read_csv["data/species.csv",
keep_default_na=False, na_values=[""]]
species_df
species_id genus species taxa
0 AB Amphispiza bilineata Bird
1 AH Ammospermophilus harrisi Rodent
2 AS Ammodramus savannarum Bird
3 BA Baiomys taylori Rodent
4 CB Campylorhynchus brunneicapillus Bird
.. ... ... ... ...
49 UP Pipilo sp. Bird
50 UR Rodent sp. Rodent
51 US Sparrow sp. Bird
52 ZL Zonotrichia leucophrys Bird
53 ZM Zenaida macroura Bird
[54 rows x 4 columns]
Hãy lưu ý rằng phương pháp
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
6 chúng tôi đã sử dụng có thể có một số tùy chọn bổ sung mà trước đây chúng tôi không sử dụng. Nhiều chức năng trong Python có một tập hợp các tùy chọn mà người dùng có thể đặt nếu cần. Trong trường hợp này, chúng tôi đã yêu cầu gấu trúc gán các giá trị trống trong CSV của chúng tôi cho NaN # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
7. Nối khung dữ liệuChúng ta có thể sử dụng hàm
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
5 trong gấu trúc để nối các cột hoặc hàng từ Khung dữ liệu này sang Khung dữ liệu khác. Hãy lấy hai tập hợp con dữ liệu của chúng tôi để xem nó hoạt động như thế nào# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
Khi chúng tôi nối các DataFrames, chúng tôi cần chỉ định trục.
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
9 yêu cầu gấu trúc xếp Khung dữ liệu thứ hai DƯỚI cái đầu tiên. Nó sẽ tự động phát hiện xem các tên cột có giống nhau hay không và sẽ xếp chồng lên nhau. # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
0 sẽ xếp các cột trong Khung dữ liệu thứ hai sang BÊN PHẢI của Khung dữ liệu đầu tiên. Để sắp xếp dữ liệu theo chiều dọc, chúng tôi cần đảm bảo rằng chúng tôi có các cột giống nhau và định dạng cột được liên kết trong cả hai bộ dữ liệu. Khi chúng tôi xếp theo chiều ngang, chúng tôi muốn đảm bảo rằng những gì chúng tôi đang làm có ý nghĩa [i. e. dữ liệu có liên quan theo một cách nào đó]________số 8_______
Giá trị chỉ mục hàng và Concat
Hãy xem khung dữ liệu
# Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
1? . Chúng tôi có thể lập chỉ mục lại khung dữ liệu mới bằng phương thức # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
4Viết dữ liệu ra CSV
Chúng tôi có thể sử dụng lệnh
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
3 để xuất DataFrame ở định dạng CSV. Lưu ý rằng mã bên dưới sẽ theo mặc định lưu dữ liệu vào thư mục làm việc hiện tại. Chúng ta có thể lưu nó vào một thư mục khác bằng cách thêm tên thư mục và dấu gạch chéo vào tệp # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
6. Chúng tôi sử dụng 'index=False' để gấu trúc không bao gồm số chỉ mục cho mỗi dòng# Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
Kiểm tra thư mục làm việc của bạn để đảm bảo CSV được viết đúng cách và bạn có thể mở nó. Nếu bạn muốn, hãy thử đưa nó trở lại Python để đảm bảo nó nhập đúng cách
# For kicks read our output back into Python and make sure all looks good
new_output = pd.read_csv['data/out.csv', keep_default_na=False, na_values=[""]]
Tham gia DataFramesThách thức - Kết hợp dữ liệu
Trong thư mục dữ liệu, có hai tệp dữ liệu khảo sát.
7 và# Stack the DataFrames on top of each other vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0] # Place the DataFrames side by side horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
8. Đọc dữ liệu vào Python và kết hợp các tệp để tạo một khung dữ liệu mới. Tạo một ô có trọng lượng ô trung bình theo năm được nhóm theo giới tính. Xuất kết quả của bạn dưới dạng CSV và đảm bảo nó đọc lại thành Python đúng cách# Stack the DataFrames on top of each other vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0] # Place the DataFrames side by side horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
Khi chúng tôi nối các Khung dữ liệu của mình, chúng tôi chỉ cần thêm chúng vào nhau - xếp chúng theo chiều dọc hoặc cạnh nhau. Một cách khác để kết hợp DataFrames là sử dụng các cột trong mỗi tập dữ liệu chứa các giá trị chung [id duy nhất chung]. Kết hợp DataFrames bằng cách sử dụng một trường chung được gọi là "tham gia". Các cột chứa các giá trị chung được gọi là “[các] khóa nối”. Tham gia DataFrames theo cách này thường hữu ích khi một DataFrame là “bảng tra cứu” chứa dữ liệu bổ sung mà chúng tôi muốn đưa vào khung kia
GHI CHÚ. Quá trình nối các bảng này tương tự như những gì chúng ta thực hiện với các bảng trong cơ sở dữ liệu SQL
Ví dụ: tệp
# Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
9 mà chúng ta đang làm việc là một bảng tra cứu. Bảng này chứa chi, loài và mã phân loại của 55 loài. Mã loài là duy nhất cho mỗi dòng. Những loài này được xác định trong dữ liệu khảo sát của chúng tôi cũng như sử dụng mã loài duy nhất. Thay vì thêm 3 cột nữa cho chi, loài và đơn vị phân loại vào mỗi bảng trong số 35.549 dòng dữ liệu Khảo sát, chúng ta có thể duy trì bảng ngắn hơn với thông tin về loài. Khi chúng tôi muốn truy cập thông tin đó, chúng tôi có thể tạo một truy vấn nối các cột thông tin bổ sung vào dữ liệu Khảo sátLưu trữ dữ liệu theo cách này có nhiều lợi ích bao gồm
- Nó đảm bảo tính thống nhất trong cách đánh vần các thuộc tính của loài [chi, loài và đơn vị phân loại] với điều kiện là mỗi loài chỉ được nhập một lần. Hãy tưởng tượng khả năng mắc lỗi chính tả khi nhập chi và loài hàng ngàn lần
- Nó cũng giúp chúng tôi dễ dàng thay đổi thông tin loài một lần mà không cần phải tìm từng trường hợp của nó trong dữ liệu khảo sát lớn hơn
- Nó tối ưu hóa kích thước dữ liệu của chúng tôi
Tham gia hai khung dữ liệu
Để hiểu rõ hơn về các phép nối, hãy lấy 10 dòng dữ liệu đầu tiên của chúng tôi làm tập hợp con để làm việc với. Chúng tôi sẽ sử dụng phương pháp
# Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
0 để làm điều này. Chúng tôi cũng sẽ đọc trong một tập hợp con của bảng loài# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
Trong ví dụ này,
# Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1 là bảng tra cứu chứa tên chi, loài và đơn vị phân loại mà chúng tôi muốn kết hợp với dữ liệu trong # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 để tạo ra một Khung dữ liệu mới chứa tất cả các cột từ cả # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
3 và # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
4Xác định các phím tham gia
Để xác định các khóa tham gia phù hợp, trước tiên chúng tôi cần biết [các] trường nào được chia sẻ giữa các tệp [DataFrames]. Chúng tôi có thể kiểm tra cả hai DataFrames để xác định các cột này. Nếu chúng tôi may mắn, cả hai DataFrames sẽ có các cột có cùng tên cũng chứa cùng một dữ liệu. Nếu chúng tôi kém may mắn hơn, chúng tôi cần xác định một cột [được đặt tên khác] trong mỗi DataFrame chứa cùng một thông tin
>>> species_sub.columns
Index[[u'species_id', u'genus', u'species', u'taxa'], dtype='object']
>>> survey_sub.columns
Index[[u'record_id', u'month', u'day', u'year', u'plot_id', u'species_id',
u'sex', u'hindfoot_length', u'weight'], dtype='object']
Trong ví dụ của chúng tôi, khóa nối là cột chứa định danh loài gồm hai chữ cái, được gọi là
# Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5Bây giờ chúng tôi đã biết các trường có thuộc tính ID loài phổ biến trong mỗi DataFrame, chúng tôi gần như đã sẵn sàng tham gia dữ liệu của mình. Tuy nhiên, vì có nhiều loại liên kết khác nhau nên chúng tôi cũng cần quyết định loại liên kết nào phù hợp với phân tích của chúng tôi
tham gia bên trong
Loại liên kết phổ biến nhất được gọi là liên kết bên trong. Một liên kết bên trong kết hợp hai DataFrames dựa trên khóa tham gia và trả về một DataFrame mới chỉ chứa những hàng có giá trị khớp trong cả hai DataFrames gốc
Các phép nối bên trong mang lại một DataFrame chỉ chứa các hàng có giá trị được nối tồn tại trong CẢ HAI bảng. Dưới đây là một ví dụ về phép nối bên trong, được điều chỉnh từ blogpost của Jeff Atwood về phép nối SQL
Hàm pandas để thực hiện phép nối được gọi là
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
4 và phép nối bên trong là tùy chọn mặc địnhmerged_inner = pd.merge[left=survey_sub, right=species_sub, left_on='species_id', right_on='species_id']
# In this case `species_id` is the only column name in both dataframes, so if we skipped `left_on`
# And `right_on` arguments we would still get the same result
# What's the size of the output data?
merged_inner.shape
merged_inner
record_id month day year plot_id species_id sex hindfoot_length \
0 1 7 16 1977 2 NL M 32
1 2 7 16 1977 3 NL M 33
2 3 7 16 1977 2 DM F 37
3 4 7 16 1977 7 DM M 36
4 5 7 16 1977 3 DM M 35
5 8 7 16 1977 1 DM M 37
6 9 7 16 1977 1 DM F 34
7 7 7 16 1977 2 PE F NaN
weight genus species taxa
0 NaN Neotoma albigula Rodent
1 NaN Neotoma albigula Rodent
2 NaN Dipodomys merriami Rodent
3 NaN Dipodomys merriami Rodent
4 NaN Dipodomys merriami Rodent
5 NaN Dipodomys merriami Rodent
6 NaN Dipodomys merriami Rodent
7 NaN Peromyscus eremicus Rodent
Kết quả của phép nối bên trong của
# Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 và # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1 là một Khung dữ liệu mới chứa tập hợp các cột được kết hợp từ # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 và # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1. Nó chỉ chứa các hàng có mã loài gồm hai chữ cái giống nhau trong cả Khung dữ liệu # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 và # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1. Nói cách khác, nếu một hàng trong # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 có giá trị # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 không xuất hiện trong cột # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 của # For kicks read our output back into Python and make sure all looks good
new_output = pd.read_csv['data/out.csv', keep_default_na=False, na_values=[""]]
6, nó sẽ không được đưa vào Khung dữ liệu được trả về bởi một phép nối bên trong. Tương tự, nếu một hàng trong # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1 có giá trị # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 không xuất hiện trong cột # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 của # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2, thì hàng đó sẽ không được đưa vào Khung dữ liệu được trả về bởi một phép nối bên trongHai DataFrames mà chúng tôi muốn tham gia được chuyển đến hàm
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
4 bằng cách sử dụng đối số # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
2 và # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
3. Đối số # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
4 yêu cầu # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
4 sử dụng cột # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 làm khóa tham gia từ # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 [Khung dữ liệu # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
2]. Tương tự như vậy, đối số # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
9 yêu cầu # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
4 sử dụng cột # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 làm khóa tham gia từ # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1 [Khung dữ liệu # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
3]. Đối với các liên kết bên trong, thứ tự của các đối số # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
2 và # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
3 không quan trọngKết quả
>>> species_sub.columns
Index[[u'species_id', u'genus', u'species', u'taxa'], dtype='object']
>>> survey_sub.columns
Index[[u'record_id', u'month', u'day', u'year', u'plot_id', u'species_id',
u'sex', u'hindfoot_length', u'weight'], dtype='object']
6 DataFrame chứa tất cả các cột từ # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 [id bản ghi, tháng, ngày, v.v. ] cũng như tất cả các cột từ # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1 [species_id, chi, loài và taxa]Lưu ý rằng
>>> species_sub.columns
Index[[u'species_id', u'genus', u'species', u'taxa'], dtype='object']
>>> survey_sub.columns
Index[[u'record_id', u'month', u'day', u'year', u'plot_id', u'species_id',
u'sex', u'hindfoot_length', u'weight'], dtype='object']
6 có ít hàng hơn # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2. Đây là dấu hiệu cho thấy có các hàng trong merged_inner = pd.merge[left=survey_sub, right=species_sub, left_on='species_id', right_on='species_id']
# In this case `species_id` is the only column name in both dataframes, so if we skipped `left_on`
# And `right_on` arguments we would still get the same result
# What's the size of the output data?
merged_inner.shape
merged_inner
1 có [các] giá trị cho # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 không tồn tại dưới dạng [các] giá trị cho # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 trong # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
3nối trái
Điều gì sẽ xảy ra nếu chúng tôi muốn thêm thông tin từ
# Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1 đến # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 mà không làm mất bất kỳ thông tin nào từ # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2? Giống như phép nối bên trong, phép nối bên trái sử dụng các phím nối để kết hợp hai DataFrames. Không giống như phép nối bên trong, phép nối bên trái sẽ trả về tất cả các hàng từ Khung dữ liệu
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
2, ngay cả những hàng có [các] khóa nối không có giá trị trong Khung dữ liệu # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
3. Các hàng trong Khung dữ liệu # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
2 thiếu giá trị cho [các] khóa tham gia trong Khung dữ liệu # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
3 sẽ chỉ có giá trị rỗng [i. e. , NaN hoặc Không có] cho các cột đó trong DataFrame đã tham gia kết quảGhi chú. nối trái vẫn sẽ loại bỏ các hàng từ Khung dữ liệu
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
3 không có giá trị cho [các] khóa nối trong Khung dữ liệu # Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Import a small subset of the species data designed for this part of the lesson.
# It is stored in the data folder.
species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
2Phép nối trái được thực hiện trong gấu trúc bằng cách gọi hàm
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
4 tương tự được sử dụng cho phép nối bên trong, nhưng sử dụng đối số record_id month day year plot_id species_id sex hindfoot_length \
0 1 7 16 1977 2 NL M 32
1 2 7 16 1977 3 NL M 33
2 3 7 16 1977 2 DM F 37
3 4 7 16 1977 7 DM M 36
4 5 7 16 1977 3 DM M 35
5 8 7 16 1977 1 DM M 37
6 9 7 16 1977 1 DM F 34
7 7 7 16 1977 2 PE F NaN
weight genus species taxa
0 NaN Neotoma albigula Rodent
1 NaN Neotoma albigula Rodent
2 NaN Dipodomys merriami Rodent
3 NaN Dipodomys merriami Rodent
4 NaN Dipodomys merriami Rodent
5 NaN Dipodomys merriami Rodent
6 NaN Dipodomys merriami Rodent
7 NaN Peromyscus eremicus Rodent
5merged_left = pd.merge[left=survey_sub, right=species_sub, how='left', left_on='species_id', right_on='species_id']
merged_left
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
0DataFrame kết quả từ một phép nối bên trái [
record_id month day year plot_id species_id sex hindfoot_length \
0 1 7 16 1977 2 NL M 32
1 2 7 16 1977 3 NL M 33
2 3 7 16 1977 2 DM F 37
3 4 7 16 1977 7 DM M 36
4 5 7 16 1977 3 DM M 35
5 8 7 16 1977 1 DM M 37
6 9 7 16 1977 1 DM F 34
7 7 7 16 1977 2 PE F NaN
weight genus species taxa
0 NaN Neotoma albigula Rodent
1 NaN Neotoma albigula Rodent
2 NaN Dipodomys merriami Rodent
3 NaN Dipodomys merriami Rodent
4 NaN Dipodomys merriami Rodent
5 NaN Dipodomys merriami Rodent
6 NaN Dipodomys merriami Rodent
7 NaN Peromyscus eremicus Rodent
6] trông rất giống với DataFrame kết quả từ một phép nối bên trong [>>> species_sub.columns
Index[[u'species_id', u'genus', u'species', u'taxa'], dtype='object']
>>> survey_sub.columns
Index[[u'record_id', u'month', u'day', u'year', u'plot_id', u'species_id',
u'sex', u'hindfoot_length', u'weight'], dtype='object']
6] về các cột mà nó chứa. Tuy nhiên, không giống như >>> species_sub.columns
Index[[u'species_id', u'genus', u'species', u'taxa'], dtype='object']
>>> survey_sub.columns
Index[[u'record_id', u'month', u'day', u'year', u'plot_id', u'species_id',
u'sex', u'hindfoot_length', u'weight'], dtype='object']
6, record_id month day year plot_id species_id sex hindfoot_length \
0 1 7 16 1977 2 NL M 32
1 2 7 16 1977 3 NL M 33
2 3 7 16 1977 2 DM F 37
3 4 7 16 1977 7 DM M 36
4 5 7 16 1977 3 DM M 35
5 8 7 16 1977 1 DM M 37
6 9 7 16 1977 1 DM F 34
7 7 7 16 1977 2 PE F NaN
weight genus species taxa
0 NaN Neotoma albigula Rodent
1 NaN Neotoma albigula Rodent
2 NaN Dipodomys merriami Rodent
3 NaN Dipodomys merriami Rodent
4 NaN Dipodomys merriami Rodent
5 NaN Dipodomys merriami Rodent
6 NaN Dipodomys merriami Rodent
7 NaN Peromyscus eremicus Rodent
6 chứa cùng số lượng hàng như Khung dữ liệu # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 ban đầu. Khi chúng tôi kiểm tra record_id month day year plot_id species_id sex hindfoot_length \
0 1 7 16 1977 2 NL M 32
1 2 7 16 1977 3 NL M 33
2 3 7 16 1977 2 DM F 37
3 4 7 16 1977 7 DM M 36
4 5 7 16 1977 3 DM M 35
5 8 7 16 1977 1 DM M 37
6 9 7 16 1977 1 DM F 34
7 7 7 16 1977 2 PE F NaN
weight genus species taxa
0 NaN Neotoma albigula Rodent
1 NaN Neotoma albigula Rodent
2 NaN Dipodomys merriami Rodent
3 NaN Dipodomys merriami Rodent
4 NaN Dipodomys merriami Rodent
5 NaN Dipodomys merriami Rodent
6 NaN Dipodomys merriami Rodent
7 NaN Peromyscus eremicus Rodent
6, chúng tôi thấy có những hàng mà thông tin lẽ ra phải đến từ # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1 [i. e. , # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5, merged_left = pd.merge[left=survey_sub, right=species_sub, how='left', left_on='species_id', right_on='species_id']
merged_left
4 và merged_left = pd.merge[left=survey_sub, right=species_sub, how='left', left_on='species_id', right_on='species_id']
merged_left
5] bị thiếu [chúng chứa các giá trị NaN]# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
1# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
2Những hàng này là những hàng mà giá trị của
# Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
5 từ # Stack the DataFrames on top of each other
vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0]
# Place the DataFrames side by side
horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
2 [trong trường hợp này là merged_left = pd.merge[left=survey_sub, right=species_sub, how='left', left_on='species_id', right_on='species_id']
merged_left
8] không xuất hiện trong # Write DataFrame to CSV
vertical_stack.to_csv['data/out.csv', index=False]
1Các loại tham gia khác
Hàm pandas
# Read in first 10 lines of surveys table
survey_sub = surveys_df.head[10]
# Grab the last 10 rows
survey_sub_last10 = surveys_df.tail[10]
# Reset the index values to the second dataframe appends properly
survey_sub_last10 = survey_sub_last10.reset_index[drop=True]
# drop=True option avoids adding new index column with old index values
4 hỗ trợ hai kiểu nối khác- Tham gia bên phải [bên ngoài]. Được gọi bằng cách chuyển
01 làm đối số. Tương tự như nối trái, ngoại trừ tất cả các hàng từ Khung dữ liệu# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Grab the last 10 rows survey_sub_last10 = surveys_df.tail[10] # Reset the index values to the second dataframe appends properly survey_sub_last10 = survey_sub_last10.reset_index[drop=True] # drop=True option avoids adding new index column with old index values
3 được giữ lại, trong khi các hàng từ Khung dữ liệu# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Import a small subset of the species data designed for this part of the lesson. # It is stored in the data folder. species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
2 không khớp với [các] giá trị khóa nối sẽ bị loại bỏ# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Import a small subset of the species data designed for this part of the lesson. # It is stored in the data folder. species_sub = pd.read_csv['data/speciesSubset.csv', keep_default_na=False, na_values=[""]]
- Tham gia đầy đủ [bên ngoài]. Được gọi bằng cách chuyển
04 làm đối số. Loại liên kết này trả về tất cả các tổ hợp hàng theo cặp từ cả hai DataFrames; . e. , DataFrame kết quả sẽ# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Grab the last 10 rows survey_sub_last10 = surveys_df.tail[10] # Reset the index values to the second dataframe appends properly survey_sub_last10 = survey_sub_last10.reset_index[drop=True] # drop=True option avoids adding new index column with old index values
05 trong đó dữ liệu bị thiếu ở một trong các khung dữ liệu. Loại tham gia này rất hiếm khi được sử dụng# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Grab the last 10 rows survey_sub_last10 = surveys_df.tail[10] # Reset the index values to the second dataframe appends properly survey_sub_last10 = survey_sub_last10.reset_index[drop=True] # drop=True option avoids adding new index column with old index values
Thách thức - Phân phối
Tạo một DataFrame mới bằng cách nối nội dung của bảng
06 và# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Grab the last 10 rows survey_sub_last10 = surveys_df.tail[10] # Reset the index values to the second dataframe appends properly survey_sub_last10 = survey_sub_last10.reset_index[drop=True] # drop=True option avoids adding new index column with old index values
9. Sau đó tính toán và vẽ biểu đồ phân phối của# Stack the DataFrames on top of each other vertical_stack = pd.concat[[survey_sub, survey_sub_last10], axis=0] # Place the DataFrames side by side horizontal_stack = pd.concat[[survey_sub, survey_sub_last10], axis=1]
- phân loại theo lô
- phân loại theo giới tính theo cốt truyện
Thách thức - Chỉ số đa dạng
- Trong thư mục dữ liệu, có một tệp
08 chứa thông tin về loại được liên kết với từng ô. Sử dụng dữ liệu đó để tóm tắt số lượng ô theo loại ô# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Grab the last 10 rows survey_sub_last10 = surveys_df.tail[10] # Reset the index values to the second dataframe appends properly survey_sub_last10 = survey_sub_last10.reset_index[drop=True] # drop=True option avoids adding new index column with old index values
Tính toán chỉ số đa dạng mà bạn chọn để kiểm soát so với các ô phát hiện loài gặm nhấm. Chỉ số nên xem xét cả sự phong phú của loài và số lượng loài. Bạn có thể chọn sử dụng chỉ số đa dạng sinh học đơn giản được mô tả ở đây để tính toán sự đa dạng như
số loài trong ô / tổng số cá thể trong ô = Chỉ số đa dạng sinh học
Những điểm chính
Pandas'
4 và# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Grab the last 10 rows survey_sub_last10 = surveys_df.tail[10] # Reset the index values to the second dataframe appends properly survey_sub_last10 = survey_sub_last10.reset_index[drop=True] # drop=True option avoids adding new index column with old index values
5 có thể được sử dụng để kết hợp các tập hợp con của DataFrame hoặc thậm chí dữ liệu từ các tệp khác nhau# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Grab the last 10 rows survey_sub_last10 = surveys_df.tail[10] # Reset the index values to the second dataframe appends properly survey_sub_last10 = survey_sub_last10.reset_index[drop=True] # drop=True option avoids adding new index column with old index values
Hàm
11 kết hợp DataFrames dựa trên chỉ mục hoặc cột# Read in first 10 lines of surveys table survey_sub = surveys_df.head[10] # Grab the last 10 rows survey_sub_last10 = surveys_df.tail[10] # Reset the index values to the second dataframe appends properly survey_sub_last10 = survey_sub_last10.reset_index[drop=True] # drop=True option avoids adding new index column with old index values
Việc tham gia hai DataFrames có thể được thực hiện theo nhiều cách [trái, phải và bên trong] tùy thuộc vào dữ liệu nào phải có trong DataFrame cuối cùng
Làm cách nào để hợp nhất 2 mảng trong Python?
Bạn có thể sử dụng numpy. hàm nối [] để nối, hợp nhất hoặc nối một chuỗi gồm hai hoặc nhiều mảng thành một mảng NumPy. Ghép nối đề cập đến việc đặt nội dung của hai hoặc nhiều mảng trong một mảng.Làm cách nào để kết hợp hai mảng NumPy?
Tham gia mảng NumPy . Trong SQL, chúng tôi nối các bảng dựa trên một khóa, trong khi ở NumPy, chúng tôi nối các mảng theo trục. Chúng tôi chuyển một chuỗi các mảng mà chúng tôi muốn nối vào hàm concatenate[], cùng với trụcLàm cách nào để nối hai chuỗi gấu trúc vào DataFrame?
Để kết hợp hai chuỗi thành một DataFrame trong Pandas, chúng ta có thể lấy hai chuỗi và nối chúng bằng phương thức concat[] .