Python in hai danh sách trong cột
Có một số cách để tham gia hoặc nối hai hoặc nhiều danh sách trong Python Show Một trong những cách dễ nhất là sử dụng toán tử 23Ví dụTham gia hai danh sách list1 = ["a", "b" , "c"] list3 = list1 + list2 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"] cho x trong list2. in (danh sách1) Tự mình thử »Hoặc bạn có thể sử dụng phương thức 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 24 để thêm list2 vào cuối list1list1 = ["a", "b" , "c"] danh sách1. gia hạn(list2) 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 DataFrameChú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
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
Sử dụng zip để nối thêm danh sách và sau đó xây dựng DFNế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
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
Đây là kết quả văn phònglương0Montreal1901Toronto1802Dallas2203NYC290Từ điển từ nhiều danh sách và chuyển đổi sang DataFramePhươ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()
Chúng tôi sẽ nhận được từ điển sau
Bây giờ chúng ta sẽ gọi pd. Khung dữ liệu. phương thức from_dict
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 26 27 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
70 71 72 73 74 75 76 77 78 79 [('Montreal', 190), ('Toronto', 180), ('Dallas', 220), ('NYC', 290)]70 Đầu ra [('Montreal', 190), ('Toronto', 180), ('Dallas', 220), ('NYC', 290)]8
con trăn 29[('Montreal', 190), ('Toronto', 180), ('Dallas', 220), ('NYC', 290)]72
26 27 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
06 07 79 75 290 291
292 79 75 295 296
79 75 290 390 27 392 296
394 79 75 397 296
79 75 290 390 27 264 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
con trăn 29 267 268 26 27____128 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 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 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
278 79 75 281 282
283 284 26 27 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
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 7
con trăn 29 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
26 27 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 79 75 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 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
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 79 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 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
Python3 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
26 27 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 28 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 27 281 296 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 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 |