Trong bài viết này, chúng tôi sẽ đề cập đến các tính năng chính của Python và SQL, những điểm tương đồng và khác biệt chính của chúng cũng như bạn nên chọn cái nào trước để bắt đầu hành trình khoa học dữ liệu của mình
Javier Canales Luna
12 phút
Dữ liệu văn bản trong Python Cheat Sheet
Chào mừng bạn đến với bảng gian lận của chúng tôi để làm việc với dữ liệu văn bản trong Python. Chúng tôi đã biên soạn một danh sách các hàm và gói hữu ích nhất để dọn dẹp, xử lý và phân tích dữ liệu văn bản trong Python, cùng với các ví dụ và giải thích rõ ràng, vì vậy bạn sẽ có mọi thứ cần biết về cách làm việc với dữ liệu văn bản trong Python.
Hướng dẫn về tập hợp và lý thuyết tập hợp trong Python
Tìm hiểu về bộ Python. chúng là gì, cách tạo chúng, khi nào sử dụng chúng, các chức năng tích hợp và mối quan hệ của chúng với các hoạt động lý thuyết thiết lập
Hướng dẫn về gấu trúc. Khung dữ liệu trong Python
Khám phá phân tích dữ liệu với Python. Pandas DataFrames giúp thao tác dữ liệu của bạn dễ dàng, từ việc chọn hoặc thay thế các cột và chỉ mục để định hình lại dữ liệu của bạn
Khi cần nối nhiều bộ dữ liệu, toán tử '+' có thể được sử dụng. Tuple là một kiểu dữ liệu không thể thay đổi. Điều đó có nghĩa là, các giá trị một khi được xác định không thể thay đổi bằng cách truy cập các phần tử chỉ mục của chúng. Nếu chúng ta cố gắng thay đổi các phần tử, nó sẽ dẫn đến lỗi. Chúng rất quan trọng chứa vì chúng đảm bảo quyền truy cập chỉ đọc
Toán tử '+' có thể được sử dụng để thêm các giá trị số hoặc nối các chuỗi
Dưới đây là một minh chứng tương tự -
Thí dụ
Bản thử trực tiếp
my_tuple_1 = [11, 14, 0, 78, 33, 11] my_tuple_2 = [10, 78, 0, 56, 8, 34] print["The first tuple is : "] print[my_tuple_1] print["The second tuple is : "] print[my_tuple_2] my_result = my_tuple_1 + my_tuple_2 print["The tuple after concatenation is : " ] print[my_result]
đầu ra
The first tuple is : [11, 14, 0, 78, 33, 11] The second tuple is : [10, 78, 0, 56, 8, 34] The tuple after concatenation is : [11, 14, 0, 78, 33, 11, 10, 78, 0, 56, 8, 34]
Giải trình
- Hai bộ dữ liệu được xác định và được hiển thị trên bảng điều khiển
- Chúng được nối bằng toán tử '+'
- Điều này được gán cho một giá trị
- Nó được hiển thị trên bảng điều khiển
Hàm lặp song song qua một số lần lặp và tạo các bộ dữ liệu với một mục từ mỗi lần lặp
Bạn có thể tưởng tượng rằng hàm zip[]
lặp qua các bộ dữ liệu, lấy 1 phần tử từ mỗi bộ dữ liệu.
Bộ đầu tiên trong danh sách bao gồm các phần tử trong mỗi bộ có chỉ số là 0
và bộ thứ hai bao gồm các phần tử trong mỗi bộ có chỉ số là 1
Bước cuối cùng là sử dụng khả năng hiểu danh sách để lặp lại đối tượng zip
và tính tổng từng bộ
Khả năng hiểu danh sách được sử dụng để thực hiện một số thao tác cho mọi phần tử hoặc chọn một tập hợp con các phần tử đáp ứng một điều kiện
Đôi khi, trong khi làm việc với các bản ghi, chúng ta có thể gặp vấn đề chung là thêm nội dung của một bộ dữ liệu với chỉ mục tương ứng của bộ dữ liệu khác. Điều này có ứng dụng trong hầu hết các lĩnh vực mà chúng tôi làm việc với các bản ghi bộ. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Phương pháp số 1. Sử dụng map[] + lambda Kết hợp các chức năng trên có thể giải quyết vấn đề cho chúng tôi. Trong phần này, chúng tôi tính tổng bằng các hàm lambda và mở rộng logic cho các khóa bằng map[].
Python3
# Python3 code to demonstrate working of
# Addition of tuples
# using map[] + lambda
# initialize tuples
test_tup1
=
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]0_______2_______1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]2
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4_______2_______5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
=
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]3
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]8
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
1The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
# Python3 code to demonstrate working of
4 The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
7
# Addition of tuples
# using map[] + lambda
# Addition of tuples
0=
# Addition of tuples
2[
___# Addition of tuples
4[
92_______6 # Addition of tuples
7
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Addition of tuples
9
# using map[] + lambda
0
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
# using map[] + lambda
3 The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# using map[] + lambda
6Đầu ra
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]
Phương pháp #2. Sử dụng map[] + zip[] + sum[] Sự kết hợp của các chức năng trên cũng có thể được sử dụng để đạt được nhiệm vụ cụ thể này. Trong phần này, chúng tôi thêm sum[] sẵn có để thực hiện tính tổng và nén các chỉ số tương tự bằng cách sử dụng zip[] và mở rộng logic cho cả hai bộ dữ liệu bằng map[].
Python3
# Python3 code to demonstrate working of
# Addition of tuples
# using map[] + lambda
9
# initialize tuples
test_tup1
=
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]0_______2_______1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]2
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4_______2_______5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
=
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]3
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]8
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
1The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
# Python3 code to demonstrate working of
4 The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
7
# Addition of tuples
# using map[] + lambda
9
# Addition of tuples
0=
# Addition of tuples
2[
___# Addition of tuples
4[
2_______00
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]02
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]03
# using map[] + lambda
0
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
# using map[] + lambda
3 The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# using map[] + lambda
6Đầu ra
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]
Phương pháp #3. Sử dụng phương thức tuple[] và vòng lặp for
Python3
# Python3 code to demonstrate working of
# Addition of tuples
# initialize tuples
test_tup1
=
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]0_______2_______1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]2
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4_______2_______5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
=
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]4
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]3
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]5
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]8
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
1The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
# Python3 code to demonstrate working of
4 The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# Python3 code to demonstrate working of
7
# Addition of tuples
# Addition of tuples
0=
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]48
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]49
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]50_______2_______51
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]52_______97_______2_______54
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]1
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]56
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]57
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]58
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]59
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]61
# Addition of tuples
0=
# Addition of tuples
2
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]65
# using map[] + lambda
0
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]6
[
# using map[] + lambda
3 The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]9
# Python3 code to demonstrate working of
0# using map[] + lambda
6Đầu ra
The original tuple 1 : [10, 4, 5] The original tuple 2 : [2, 5, 18] Resultant tuple after addition : [12, 9, 23]
Phương pháp #4. sử dụng numpy.
Ghi chú. Cài đặt mô-đun numpy bằng lệnh “pip install numpy”
Chúng ta có thể sử dụng thư viện numpy để thêm hai bộ dữ liệu, trước tiên chúng ta sẽ chuyển đổi bộ dữ liệu thành mảng có nhiều mảng và sau đó sử dụng chức năng dựng sẵn của numpy 'add' để thêm các bộ dữ liệu