Có một số cách để tham gia hoặc nối hai hoặc nhiều danh sách trong Python
Một trong những cách dễ nhất là sử dụng toán tử
hr_lst = list [zip[office, avg_salary]]
hr_lst
23Ví dụ
Tham gia hai danh sách
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print[list3]
Một cách khác để nối hai danh sách là nối thêm tất cả các mục từ danh sách 2 vào danh sách 1, từng mục một
Ví dụ
Nối list2 vào list1
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
cho x trong list2.
danh sách1. nối thêm[x]
in [danh sách1]
Tự mình thử »Hoặc bạn có thể sử dụng phương thức
hr_lst = list [zip[office, avg_salary]]
hr_lst
24, mục đích là thêm các phần tử từ danh sách này sang danh sách khácVí dụ
Sử dụng phương thức
hr_lst = list [zip[office, avg_salary]]
hr_lst
24 để thêm list2 vào cuối list1list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
danh sách1. gia hạn[list2]
print[list1]
Trong hướng dẫn ngắn này, chúng ta sẽ tìm hiểu cách sử dụng thư viện Python và Pandas để dễ dàng kết hợp và chuyển đổi một số danh sách thành DataFrame. Trong ví dụ này, chúng tôi sẽ chỉ ra cách kết hợp các danh sách lại với nhau để mỗi danh sách sẽ đại diện cho một cột khác trong DataFrame
Ví dụ về nhiều danh sách cho DataFrame
Chúng tôi sẽ xem xét hai cách khác nhau để chuyển đổi danh sách danh sách của chúng tôi thành đối tượng DataFrame Python
- Nén danh sách và sau đó xây dựng DF
- Xác định từ điển trước rồi xây dựng DataFrame
Các bước chuẩn bị
Chúng tôi sẽ nhanh chóng nhập thư viện Pandas vào môi trường phát triển Python của mình, sau đó xác định hai đối tượng danh sách mà chúng tôi sẽ sử dụng trong ví dụ này
import pandas as pd
office = ['Montreal', 'Toronto', 'Dallas', 'NYC']
avg_salary = [190, 180, 220, 290]
Sử dụng zip để nối thêm danh sách và sau đó xây dựng DF
Nếu chúng ta muốn lấy dữ liệu của từng danh sách vào các cột khác nhau thì trước tiên chúng ta phải xác định một danh sách các danh sách. Mỗi danh sách sẽ đại diện cho một hàng trong DataFrame. Chức năng zip siêu tiện dụng cho phép chúng tôi ghép các danh sách lại với nhau
hr_lst = list [zip[office, avg_salary]]
hr_lst
Chúng tôi sẽ nhận được một danh sách các danh sách
[['Montreal', 190], ['Toronto', 180], ['Dallas', 220], ['NYC', 290]]
Bây giờ chúng ta sẽ xây dựng đối tượng DataFrame
hr_df_1 = pd.DataFrame[hr_lst, columns = ['office', 'salary']]
hr_df_1.head[]
Đây là kết quả
văn phònglương0Montreal1901Toronto1802Dallas2203NYC290Từ điển từ nhiều danh sách và chuyển đổi sang DataFrame
Phương pháp thứ hai chúng ta sẽ nhanh chóng sử dụng để xây dựng DF của mình từ nhiều đối tượng danh sách là sử dụng lệnh pd. Khung dữ liệu. phương thức from_dict
Trước tiên chúng ta sẽ định nghĩa một từ điển đơn giản bằng cách sử dụng hàm dict[]
hr_dict = dict[office=office, salary =avg_salary]
Chúng tôi sẽ nhận được từ điển sau
{'office': ['Montreal', 'Toronto', 'Dallas', 'NYC'],
'salary': [190, 180, 220, 290]}
Bây giờ chúng ta sẽ gọi pd. Khung dữ liệu. phương thức from_dict
hr_df_2 = pd.DataFrame.from_dict[hr_dict]
hr_df_2.head[]
Kết quả tương tự như trước [lưu ý rằng trong trường hợp này không cần chỉ định tên cột, chúng được tự động suy ra từ từ điển
hr_lst = list [zip[office, avg_salary]]
hr_lst
26hr_lst = list [zip[office, avg_salary]]
hr_lst
27 hr_lst = list [zip[office, avg_salary]]
hr_lst
281 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 50
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 52
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 54
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 56
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 58
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 59
hr_lst = list [zip[office, avg_salary]]
hr_lst
70hr_lst = list [zip[office, avg_salary]]
hr_lst
71 hr_lst = list [zip[office, avg_salary]]
hr_lst
72hr_lst = list [zip[office, avg_salary]]
hr_lst
73 hr_lst = list [zip[office, avg_salary]]
hr_lst
74hr_lst = list [zip[office, avg_salary]]
hr_lst
75hr_lst = list [zip[office, avg_salary]]
hr_lst
76hr_lst = list [zip[office, avg_salary]]
hr_lst
77hr_lst = list [zip[office, avg_salary]]
hr_lst
78hr_lst = list [zip[office, avg_salary]]
hr_lst
79 [['Montreal', 190], ['Toronto', 180], ['Dallas', 220], ['NYC', 290]]70
Đầu ra
[['Montreal', 190], ['Toronto', 180], ['Dallas', 220], ['NYC', 290]]8
- Không sử dụng vòng lặp. * biểu tượng được sử dụng để in các phần tử danh sách trong một dòng có khoảng trắng. Để in tất cả các phần tử trong các dòng mới hoặc được phân tách bằng dấu phẩy, hãy sử dụng sep=”\n” hoặc sep=”, ” tương ứng.
con trăn
hr_lst = list [zip[office, avg_salary]]
hr_lst
29[['Montreal', 190], ['Toronto', 180], ['Dallas', 220], ['NYC', 290]]72
hr_lst = list [zip[office, avg_salary]]
hr_lst
26hr_lst = list [zip[office, avg_salary]]
hr_lst
27 hr_lst = list [zip[office, avg_salary]]
hr_lst
281 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 50
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 52
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 54
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 56
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 58
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 59
hr_dict = dict[office=office, salary =avg_salary]
06hr_dict = dict[office=office, salary =avg_salary]
07hr_lst = list [zip[office, avg_salary]]
hr_lst
79hr_lst = list [zip[office, avg_salary]]
hr_lst
75hr_lst = list [zip[office, avg_salary]]
hr_lst
290hr_lst = list [zip[office, avg_salary]]
hr_lst
291
hr_lst = list [zip[office, avg_salary]]
hr_lst
292hr_lst = list [zip[office, avg_salary]]
hr_lst
79hr_lst = list [zip[office, avg_salary]]
hr_lst
75hr_lst = list [zip[office, avg_salary]]
hr_lst
295hr_lst = list [zip[office, avg_salary]]
hr_lst
296
hr_lst = list [zip[office, avg_salary]]
hr_lst
79hr_lst = list [zip[office, avg_salary]]
hr_lst
75hr_lst = list [zip[office, avg_salary]]
hr_lst
290hr_lst = list [zip[office, avg_salary]]
hr_lst
390hr_lst = list [zip[office, avg_salary]]
hr_lst
27 hr_lst = list [zip[office, avg_salary]]
hr_lst
392hr_lst = list [zip[office, avg_salary]]
hr_lst
296
hr_lst = list [zip[office, avg_salary]]
hr_lst
394hr_lst = list [zip[office, avg_salary]]
hr_lst
79hr_lst = list [zip[office, avg_salary]]
hr_lst
75hr_lst = list [zip[office, avg_salary]]
hr_lst
397hr_lst = list [zip[office, avg_salary]]
hr_lst
296
hr_lst = list [zip[office, avg_salary]]
hr_lst
79hr_lst = list [zip[office, avg_salary]]
hr_lst
75hr_lst = list [zip[office, avg_salary]]
hr_lst
290hr_lst = list [zip[office, avg_salary]]
hr_lst
390hr_lst = list [zip[office, avg_salary]]
hr_lst
27 hr_lst = list [zip[office, avg_salary]]
hr_lst
264hr_lst = list [zip[office, avg_salary]]
hr_lst
296Đầu ra
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 5
- Chuyển đổi một danh sách thành một chuỗi để hiển thị. Nếu đó là một danh sách các chuỗi, chúng ta chỉ cần nối chúng bằng hàm join[], nhưng nếu danh sách chứa các số nguyên thì hãy chuyển đổi nó thành chuỗi và sau đó sử dụng hàm join[] để nối chúng thành một chuỗi và in chuỗi đó.
con trăn
hr_lst = list [zip[office, avg_salary]]
hr_lst
29hr_lst = list [zip[office, avg_salary]]
hr_lst
267hr_lst = list [zip[office, avg_salary]]
hr_lst
268hr_lst = list [zip[office, avg_salary]]
hr_lst
26hr_lst = list [zip[office, avg_salary]]
hr_lst
27____128hr_lst = list [zip[office, avg_salary]]
hr_lst
2721 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
hr_lst = list [zip[office, avg_salary]]
hr_lst
2741 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
hr_lst = list [zip[office, avg_salary]]
hr_lst
2721 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 59
hr_lst = list [zip[office, avg_salary]]
hr_lst
278hr_lst = list [zip[office, avg_salary]]
hr_lst
79hr_lst = list [zip[office, avg_salary]]
hr_lst
75hr_lst = list [zip[office, avg_salary]]
hr_lst
281hr_lst = list [zip[office, avg_salary]]
hr_lst
282
hr_lst = list [zip[office, avg_salary]]
hr_lst
283hr_lst = list [zip[office, avg_salary]]
hr_lst
284hr_lst = list [zip[office, avg_salary]]
hr_lst
26hr_lst = list [zip[office, avg_salary]]
hr_lst
27 hr_lst = list [zip[office, avg_salary]]
hr_lst
281 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 50
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 52
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 54
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 56
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 58
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 59
hr_lst = list [zip[office, avg_salary]]
hr_lst
79 1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 509______710
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 50
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 512
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 513
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 50
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 59
Đầu ra
hr_lst = list [zip[office, avg_salary]]
hr_lst
7- Sử dụng bản đồ. Sử dụng map[] để chuyển đổi từng mục trong danh sách thành chuỗi nếu danh sách không phải là chuỗi, sau đó nối chúng.
con trăn
hr_lst = list [zip[office, avg_salary]]
hr_lst
29hr_lst = list [zip[office, avg_salary]]
hr_lst
2831 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 518
hr_lst = list [zip[office, avg_salary]]
hr_lst
26hr_lst = list [zip[office, avg_salary]]
hr_lst
27 hr_lst = list [zip[office, avg_salary]]
hr_lst
281 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 50
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 52
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 54
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 56
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 58
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 59
hr_lst = list [zip[office, avg_salary]]
hr_lst
79hr_lst = list [zip[office, avg_salary]]
hr_lst
75hr_lst = list [zip[office, avg_salary]]
hr_lst
2811 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 535
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 536
hr_lst = list [zip[office, avg_salary]]
hr_lst
751 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 509
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 539
hr_lst = list [zip[office, avg_salary]]
hr_lst
791 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 541
hr_lst = list [zip[office, avg_salary]]
hr_lst
79hr_lst = list [zip[office, avg_salary]]
hr_lst
751 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 544
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 535
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 536
hr_lst = list [zip[office, avg_salary]]
hr_lst
751 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 509
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 539
Đầu ra
[['Montreal', 190], ['Toronto', 180], ['Dallas', 220], ['NYC', 290]]7
- Sử dụng hiểu danh sách. Sử dụng khả năng hiểu danh sách để đi từng phần tử một trong danh sách và in.
Python3
hr_lst = list [zip[office, avg_salary]]
hr_lst
291 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 551
hr_lst = list [zip[office, avg_salary]]
hr_lst
26hr_lst = list [zip[office, avg_salary]]
hr_lst
27 hr_lst = list [zip[office, avg_salary]]
hr_lst
281 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 50
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 52
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 54
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 56
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 51
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 58
1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 59
hr_lst = list [zip[office, avg_salary]]
hr_lst
28hr_lst = list [zip[office, avg_salary]]
hr_lst
791 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 567
hr_lst = list [zip[office, avg_salary]]
hr_lst
27hr_lst = list [zip[office, avg_salary]]
hr_lst
281hr_lst = list [zip[office, avg_salary]]
hr_lst
296hr_lst = list [zip[office, avg_salary]]
hr_lst
71 1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 572
hr_lst = list [zip[office, avg_salary]]
hr_lst
73 1 2 3 4 5 printing lists separated by commas 1, 2, 3, 4, 5 printing lists in new line 1 2 3 4 574