Bạn có thể tạo một cột có điều kiện trong DataFrame của gấu trúc bằng cách sử dụng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
0,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
1,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
2,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
3,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
4,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
5. Ngoài ra, bạn cũng có thể sử dụng các hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
7 và
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
8 của phương thức
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
6 để tạo các hàm đơn và nhiều hàm. Trong bài viết này, tôi sẽ giải thích một số cách tạo cột DataFrame có điều kiện [mới] với các ví dụCần thêm một cột mới bằng cách kiểm tra có điều kiện các giá trị trên các cột hiện có khi bạn cần sắp xếp DataFrame hoặc lấy một cột mới từ các cột hiện có
1. Ví dụ nhanh về Pandas Tạo cột DataFrame có điều kiện
Nếu bạn đang vội, dưới đây là một số ví dụ nhanh
# Below are some quick examples.
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
# Another way to create column conditionally.
df['Discount'] = [1000 if x == 'Spark' else 2000 for x in df['Courses']]
# Create conditional DataFrame column by map[] and lambda.
df['Discount'] = df.Courses.map[ lambda x: 1000 if x == 'Spark' else 2000]
# Create conditional DataFrame column by np.select[] function.
conditions = [
[df['Courses'] == 'Spark'] & [df['Duration'] == '30days'],
[df['Courses'] == 'Spark'] & [df['Duration'] == '35days'],
[df['Duration'] == '50days']]
choices = [1000, 1050,200]
df['Discount'] = np.select[conditions,choices, default=0]
# Using Dictionary to map new values.
Discount_dictionary ={'Spark' : 1500, 'PySpark' : 800, 'Spark' : 1200}
df['Discount'] = df['Courses'].map[Discount_dictionary]
# Pandas create conditional DataFrame column by dictionary
df['Discount'] = [Discount_dictionary.get[v, None] for v in df['Courses']]
# Using DataFrame.assign[] method.
def Courses_Discount[row]:
if row["Courses"] == "Spark":
return 1000
else:
return 2000
df = df.assign[Discount=df.apply[Courses_Discount, axis=1]]
# Conditions with multiple rand multiple columns.
def Courses_Discount[row]:
if row["Courses"] == "Spark":
return 1000
elif row["Fee"] == 25000:
return 2000
else:
return 0
df = df.assign[Discount=df.apply[Courses_Discount, axis=1]]
# Using .loc[] property for single condition.
df.loc[[df['Courses']=="Spark"], 'Discount'] = 1000
# Using loc[] method for Multiple conditions.
df.loc[[df['Courses']=="Spark"]&[df['Fee']==23000]|[df['Fee']==25000], 'Discount'] = 1000
# Using DataFrame.apply[] method with lambda function.
df['Discount'] = df['Courses'].apply[lambda x: '1000' if x=='Spark' else 1000]
# Pandas create conditional column using mask[] method.
# Replace values where the condition is True
df['Discount'] = df['Discount'].mask[df['Courses']=='Spark', other=1000]
# Using where[]
df['Discount'] = df['Discount'].where[df['Courses']=='Spark', other=1000]
# Using transform[] with a lambda function.
df['Discount'] = df['Courses'].transform[lambda x: 1000 if x == 'Spark' else 2000]
Hãy tạo một DataFrame gấu trúc với một vài hàng và cột và thực hiện các ví dụ này và xác thực kết quả. Khung dữ liệu của chúng tôi chứa các tên cột
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
9,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
10 và
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
11
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
3Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
42. Tạo cột DataFrame có điều kiện bằng np. hàm where[]
Để tạo cột DataFrame có điều kiện trong gấu trúc, hãy sử dụng hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
0. Mặc dù gấu trúc cũng có hàm where[] tương tự, nhưng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
13 lại rất khác. Sự khác biệt là, chức năng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
14 của
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
15 cung cấp tính linh hoạt cao hơn và nó xử lý điều kiện đã cho khác với Pandashàm pandas
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
14 chỉ cho phép cập nhật các giá trị không đáp ứng điều kiện đã cho. Tuy nhiên, hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
17 của
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
15 cho phép cập nhật các giá trị thỏa mãn và không thỏa mãn điều kiện đã cho
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
1Một cách khác để tạo một cột có điều kiện
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
4Mang lại sản lượng tương tự như trên
Tương tự, bạn cũng có thể tạo bằng cách sử dụng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
19 và
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
8.
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
41 được xác định bằng từ khóa
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
8. Họ có thể có bất kỳ số đối số nhưng chỉ có một biểu thức. Chúng rất hữu ích khi chúng ta phải thực hiện các tác vụ nhỏ với ít mã hơn
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
9Mang lại sản lượng tương tự như trên
3. Tạo cột DataFrame có điều kiện bằng numpy. chức năng chọn []
Bạn có thể tạo cột DataFrame có điều kiện bằng cách kiểm tra nhiều cột bằng hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
43. Hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
44 có nhiều khả năng hơn các phương thức trước đó. Chúng ta có thể sử dụng nó để đưa ra một tập hợp các điều kiện và một tập hợp các giá trị. Do đó, chúng ta có thể gán một giá trị cụ thể cho từng điều kiệnKhi không có điều kiện nào phù hợp, nó sẽ gán giá trị mặc định cho cột mới
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
2Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
34. Sử dụng Khung dữ liệu. bản đồ [] Chức năng
Chúng ta có thể sử dụng hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
2 để đạt được mục tiêu tương tự. Đó là một phương pháp đơn giản trong đó chúng tôi sử dụng từ điển để chỉ cần ____246 giá trị cho cột mới được thêm vào dựa trên khóa. Giá trị
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
47 của
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
48 theo tương ứng đầu vào. Nó được sử dụng để thay thế
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
49
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
90 trong Sê-ri bằng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
91
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
1Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
305. Sử dụng Dict để tạo cột DataFrame có điều kiện
Một phương pháp khác để tạo cột DataFrame có điều kiện của gấu trúc là tạo một Dict với cặp khóa-giá trị.
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
92. Phương thức ____293 trả về giá trị của mục với khóa được chỉ định. Tuy nhiên, nếu ______294 không được tìm thấy khi bạn sử dụng ______295 thì nó sẽ gán ____296
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
31Mang lại sản lượng tương tự như trên
6. Sử dụng sê-ri. áp dụng [] chức năng
Chúng tôi có thể sử dụng hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
97, khi chúng tôi có nhiều hơn hai giá trị, trong trường hợp đó, chúng tôi có thể sử dụng một từ điển để ánh xạ các giá trị mới vào các khóa. Nó cung cấp rất nhiều tính linh hoạt khi chúng tôi có số lượng danh mục lớn hơn mà chúng tôi muốn gán các giá trị khác nhau cho cột mới được thêm vào
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
32Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
337. Sử dụng Khung dữ liệu. phương thức gán []
Hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
98] được sử dụng để gán các cột mới cho một
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
99. Trả về một đối tượng mới với tất cả các cột ban đầu cùng với các cột mới. Lưu ý rằng tất cả các ví dụ trên đều tạo một cột mới trên DataFrame hiện có, ví dụ này tạo một DataFrame mới với cột mới
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
34Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
18. Sử dụng nhiều cột bằng cách sử dụng DataFrame. phương thức gán []
Nếu bạn cần kiểm tra nhiều cột để tạo cột mới, hãy sử dụng hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
3, bạn có thể xem ví dụ bên dưới-
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
36Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
379. Sử dụng Khung dữ liệu. loc[] Phương pháp
Thuộc tính
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
21 được sử dụng để truy cập một nhóm hàng và cột theo [các] nhãn hoặc mảng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
22.
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
21 chủ yếu dựa trên nhãn, nhưng cũng có thể được sử dụng với mảng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
22. Bạn có thể áp dụng thuộc tính
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
21 cho một điều kiện-
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
38Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
39GHI CHÚ. Ngoài ra, để áp dụng thuộc tính
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
26 cho nhiều điều kiện và tạo một cột mới trong DataFrame của gấu trúc. Ví dụ
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
40Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
4110. Sử dụng Khung dữ liệu. phương thức apply[] với hàm lambda
Bạn cũng có thể tạo cột DataFrame có điều kiện bằng cách sử dụng phương thức
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
4 với hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
8. Hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
29 dọc theo một trục của
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
99. Hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
8 được xác định bằng từ khóa
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
8
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
42Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
4311. Pandas Tạo cột có điều kiện bằng phương thức Mask[]
Hãy xem bằng cách sử dụng phương pháp
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
6. Phương pháp
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
6 được sử dụng để thay thế các giá trị có điều kiện là
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
35
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
44Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
43GHI CHÚ. Bạn có thể thay thế các giá trị có điều kiện là
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
36 bằng phương pháp
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
37. Phương pháp ________ 114 được sử dụng để kiểm tra ________ 299 đối với một hoặc nhiều điều kiện và trả về kết quả tương ứng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
46Mang lại sản lượng tương tự như trên
12. Sử dụng biến đổi [] với hàm lambda
Cuối cùng, bạn có thể sử dụng phương thức
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
7 với hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
8. Hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
7 trả về DataFrame tự tạo với các giá trị được chuyển đổi sau khi áp dụng hàm được chỉ định trong tham số của nó
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
47Sản lượng dưới sản lượng
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
113. Ví dụ hoàn chỉnh cho Pandas Tạo cột có điều kiện
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
49Phần kết luận
Trong bài viết này, bạn đã học cách Pandas tạo cột có điều kiện DataFrame bằng cách sử dụng phương thức
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
0,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
1,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
4,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
3,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
2,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
21,
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
6, các hàm
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
7 và
import numpy as np
# Create conditional DataFrame column by np.where[] function.
df['Discount'] = np.where[df['Courses']=='Spark', 1000, 2000]
print[df]
8 để tạo các hàm đơn và nhiều hàm