Trong
print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
1, tham số đầu tiên s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
5 là mảng một chiều [danh sách Python hoặc s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
6, s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
2] làm dữ liệu nguồn và tham số thứ hai s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
8 là cài đặt phân chia binChỉ định số lượng thùng có chiều rộng bằng nhau
Bạn có thể chỉ định số ngăn có chiều rộng bằng nhau bằng cách chỉ định giá trị số nguyên cho
s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
8s_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
nguồn.
print[pd.cut[s, 4, right=False]]
# a [0.0, 25.0]
# b [0.0, 25.0]
# c [0.0, 25.0]
# d [0.0, 25.0]
# e [0.0, 25.0]
# f [25.0, 50.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.1]
# k [75.0, 100.1]
# dtype: category
# Categories [4, interval[float64]]: [[0.0, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.1]]
0 có nghĩa là print[pd.cut[s, 4, right=False]]
# a [0.0, 25.0]
# b [0.0, 25.0]
# c [0.0, 25.0]
# d [0.0, 25.0]
# e [0.0, 25.0]
# f [25.0, 50.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.1]
# k [75.0, 100.1]
# dtype: category
# Categories [4, interval[float64]]: [[0.0, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.1]]
1Chỉ định các cạnh thùng [giá trị biên]
Bạn có thể chỉ định các cạnh của thùng, các giá trị biên, bằng cách chỉ định một danh sách cho
s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
8. Các yếu tố bên ngoài phạm vi được coi là print[pd.cut[s, 4, right=False]]
# a [0.0, 25.0]
# b [0.0, 25.0]
# c [0.0, 25.0]
# d [0.0, 25.0]
# e [0.0, 25.0]
# f [25.0, 50.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.1]
# k [75.0, 100.1]
# dtype: category
# Categories [4, interval[float64]]: [[0.0, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.1]]
3print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
nguồn.
Nhận các thùng được tính toán hoặc được chỉ định. print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
6
print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
Với
print[pd.cut[s, 4, right=False]]
# a [0.0, 25.0]
# b [0.0, 25.0]
# c [0.0, 25.0]
# d [0.0, 25.0]
# e [0.0, 25.0]
# f [25.0, 50.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.1]
# k [75.0, 100.1]
# dtype: category
# Categories [4, interval[float64]]: [[0.0, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.1]]
5, bạn có thể nhận được thùng, tôi. e. , một danh sách các giá trị biên ngoài dữ liệu được đánh dấus_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
nguồn.
Chỉ định có bao gồm cạnh ngoài cùng bên phải hay không. print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
7
print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
Theo mặc định, cạnh ngoài cùng bên phải được bao gồm và cạnh ngoài cùng bên trái không. Ngược lại, nếu
print[pd.cut[s, 4, right=False]]
# a [0.0, 25.0]
# b [0.0, 25.0]
# c [0.0, 25.0]
# d [0.0, 25.0]
# e [0.0, 25.0]
# f [25.0, 50.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.1]
# k [75.0, 100.1]
# dtype: category
# Categories [4, interval[float64]]: [[0.0, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.1]]
7, cạnh ngoài cùng bên phải không được bao gồmprint[pd.cut[s, 4, right=False]]
# a [0.0, 25.0]
# b [0.0, 25.0]
# c [0.0, 25.0]
# d [0.0, 25.0]
# e [0.0, 25.0]
# f [25.0, 50.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.1]
# k [75.0, 100.1]
# dtype: category
# Categories [4, interval[float64]]: [[0.0, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.1]]
nguồn.
Chỉ định nhãn. print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
8
print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
Bạn có thể chỉ định nhãn với tham số
print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
8. Các nhãn mặc định giống như print[pd.cut[s, 4, right=False]]
# a [0.0, 25.0]
# b [0.0, 25.0]
# c [0.0, 25.0]
# d [0.0, 25.0]
# e [0.0, 25.0]
# f [25.0, 50.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.1]
# k [75.0, 100.1]
# dtype: category
# Categories [4, interval[float64]]: [[0.0, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.1]]
0, như trong các ví dụ trướcNếu
print[pd.cut[s, 4, labels=False]]
# a 0
# b 0
# c 0
# d 0
# e 0
# f 0
# g 1
# h 1
# i 2
# j 3
# k 3
# dtype: int64
1, chỉ mục số nguyên [số thứ tự bắt đầu từ 0] được sử dụng làm nhãnprint[pd.cut[s, 4, labels=False]]
# a 0
# b 0
# c 0
# d 0
# e 0
# f 0
# g 1
# h 1
# i 2
# j 3
# k 3
# dtype: int64
nguồn.
Bạn cũng có thể chỉ định danh sách bất kỳ nhãn nào. Lưu ý rằng sẽ xảy ra lỗi nếu số lượng ngăn không khớp với số lượng phần tử trong danh sách
print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
nguồn.
Chỉ định độ chính xác của các giá trị biên. print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
9
print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
Bạn có thể chỉ định độ chính xác [số vị trí thập phân] của các giá trị ranh giới với tham số
print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
9print[pd.cut[s, 3]]
# a [-0.1, 33.333]
# b [-0.1, 33.333]
# c [-0.1, 33.333]
# d [-0.1, 33.333]
# e [-0.1, 33.333]
# f [-0.1, 33.333]
# g [33.333, 66.667]
# h [33.333, 66.667]
# i [33.333, 66.667]
# j [66.667, 100.0]
# k [66.667, 100.0]
# dtype: category
# Categories [3, interval[float64]]: [[-0.1, 33.333] < [33.333, 66.667] < [66.667, 100.0]]
print[pd.cut[s, 3, precision=1]]
# a [-0.1, 33.3]
# b [-0.1, 33.3]
# c [-0.1, 33.3]
# d [-0.1, 33.3]
# e [-0.1, 33.3]
# f [-0.1, 33.3]
# g [33.3, 66.7]
# h [33.3, 66.7]
# i [33.3, 66.7]
# j [66.7, 100.0]
# k [66.7, 100.0]
# dtype: category
# Categories [3, interval[float64]]: [[-0.1, 33.3] < [33.3, 66.7] < [66.7, 100.0]]
nguồn.
Binning để làm cho số phần tử bằng nhau. s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
0
s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
print[pd.cut[s, 4, labels=False]]
# a 0
# b 0
# c 0
# d 0
# e 0
# f 0
# g 1
# h 1
# i 2
# j 3
# k 3
# dtype: int64
5 phân chia dữ liệu sao cho số phần tử trong mỗi ngăn bằng nhau nhất có thểTham số đầu tiên
s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
5 là mảng một chiều [danh sách Python hoặc s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
6, s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
2] làm dữ liệu nguồn và tham số thứ hai print[pd.cut[s, 4, labels=False]]
# a 0
# b 0
# c 0
# d 0
# e 0
# f 0
# g 1
# h 1
# i 2
# j 3
# k 3
# dtype: int64
9 là số lượng thùngBạn có thể chỉ định các tham số giống như trong
print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
0, print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
8 và print[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
6Chỉ định số lượng thùng
Bạn có thể chỉ định số lượng phân chia trong
print[pd.cut[s, 4, labels=False]]
# a 0
# b 0
# c 0
# d 0
# e 0
# f 0
# g 1
# h 1
# i 2
# j 3
# k 3
# dtype: int64
9Nếu
print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
4, dữ liệu được chia cho 2 phân vị [trung vị]print[pd.qcut[s, 2]]
# a [-0.001, 25.0]
# b [-0.001, 25.0]
# c [-0.001, 25.0]
# d [-0.001, 25.0]
# e [-0.001, 25.0]
# f [-0.001, 25.0]
# g [25.0, 100.0]
# h [25.0, 100.0]
# i [25.0, 100.0]
# j [25.0, 100.0]
# k [25.0, 100.0]
# dtype: category
# Categories [2, interval[float64]]: [[-0.001, 25.0] < [25.0, 100.0]]
nguồn.
Nếu
print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
5, dữ liệu được chia cho 4-phân vị [phần tư]s_qcut, bins = pd.qcut[s, 4, labels=['Q1', 'Q2', 'Q3', 'Q4'], retbins=True]
print[s_qcut]
# a Q1
# b Q1
# c Q1
# d Q2
# e Q2
# f Q2
# g Q3
# h Q3
# i Q4
# j Q4
# k Q4
# dtype: category
# Categories [4, object]: [Q1 < Q2 < Q3 < Q4]
print[bins]
# [ 0. 6.5 25. 56.5 100. ]
nguồn.
Đối với các giá trị trùng lặp
Cẩn thận khi có các phần tử trùng lặp trong dữ liệu gốc
Ví dụ: nếu các giá trị được nhân đôi lên đến trung bình
s_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
0nguồn.
Có thể chia cho 2 ở trung vị là
print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
4, nhưng số lượng chia nhiều hơn sẽ dẫn đến lỗis_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
1nguồn.
Ví dụ: trong trường hợp có 4 phần chia, các giá trị tối thiểu, 25%, 50%, 75% và tối đa được đặt làm giá trị biên, nhưng nếu có nhiều phần tử chồng chéo như trong ví dụ thì giá trị tối thiểu và 25% là
Nếu
print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
7, các giá trị ranh giới trùng lặp sẽ bị loại trừs_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
2nguồn.
Đếm số phần tử trong thùng. s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
1
s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
Bạn có thể lấy số phần tử trong một thùng bằng cách gọi phương thức
s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
1 từ phương thức s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
2 được trả về bởi print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
0 hoặc print[pd.cut[s, 4, labels=False]]
# a 0
# b 0
# c 0
# d 0
# e 0
# f 0
# g 1
# h 1
# i 2
# j 3
# k 3
# dtype: int64
5- gấu trúc. Loạt. value_counts — gấu trúc 1. 4. 3 tài liệu
s_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
3nguồn.
s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
1 cũng được cung cấp dưới dạng hàm print[pd.cut[s, 3]]
# a [-0.1, 33.333]
# b [-0.1, 33.333]
# c [-0.1, 33.333]
# d [-0.1, 33.333]
# e [-0.1, 33.333]
# f [-0.1, 33.333]
# g [33.333, 66.667]
# h [33.333, 66.667]
# i [33.333, 66.667]
# j [66.667, 100.0]
# k [66.667, 100.0]
# dtype: category
# Categories [3, interval[float64]]: [[-0.1, 33.333] < [33.333, 66.667] < [66.667, 100.0]]
print[pd.cut[s, 3, precision=1]]
# a [-0.1, 33.3]
# b [-0.1, 33.3]
# c [-0.1, 33.3]
# d [-0.1, 33.3]
# e [-0.1, 33.3]
# f [-0.1, 33.3]
# g [33.3, 66.7]
# h [33.3, 66.7]
# i [33.3, 66.7]
# j [66.7, 100.0]
# k [66.7, 100.0]
# dtype: category
# Categories [3, interval[float64]]: [[-0.1, 33.3] < [33.3, 66.7] < [66.7, 100.0]]
4s_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
4nguồn.
Liên kết được tài trợ
Đối với danh sách Python và mảng NumPy
Các ví dụ trước đã sử dụng
s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
2 làm dữ liệu nguồn, nhưng tham số đầu tiên s_cut, bins = pd.cut[s, 4, retbins=True]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[bins]
print[type[bins]]
# [ -0.1 25. 50. 75. 100. ]
#
5 của print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
0 hoặc print[pd.cut[s, 4, labels=False]]
# a 0
# b 0
# c 0
# d 0
# e 0
# f 0
# g 1
# h 1
# i 2
# j 3
# k 3
# dtype: int64
5 có thể là danh sách Python hoặc mảng NumPy print[pd.cut[s, 3]]
# a [-0.1, 33.333]
# b [-0.1, 33.333]
# c [-0.1, 33.333]
# d [-0.1, 33.333]
# e [-0.1, 33.333]
# f [-0.1, 33.333]
# g [33.333, 66.667]
# h [33.333, 66.667]
# i [33.333, 66.667]
# j [66.667, 100.0]
# k [66.667, 100.0]
# dtype: category
# Categories [3, interval[float64]]: [[-0.1, 33.333] < [33.333, 66.667] < [66.667, 100.0]]
print[pd.cut[s, 3, precision=1]]
# a [-0.1, 33.3]
# b [-0.1, 33.3]
# c [-0.1, 33.3]
# d [-0.1, 33.3]
# e [-0.1, 33.3]
# f [-0.1, 33.3]
# g [33.3, 66.7]
# h [33.3, 66.7]
# i [33.3, 66.7]
# j [66.7, 100.0]
# k [66.7, 100.0]
# dtype: category
# Categories [3, interval[float64]]: [[-0.1, 33.3] < [33.3, 66.7] < [66.7, 100.0]]
9 nếu nó là một chiềus_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
5nguồn.
Bạn có thể lấy các phần tử theo chỉ mục và chuyển đổi chúng thành danh sách Python với
print[pd.qcut[s, 2]]
# a [-0.001, 25.0]
# b [-0.001, 25.0]
# c [-0.001, 25.0]
# d [-0.001, 25.0]
# e [-0.001, 25.0]
# f [-0.001, 25.0]
# g [25.0, 100.0]
# h [25.0, 100.0]
# i [25.0, 100.0]
# j [25.0, 100.0]
# k [25.0, 100.0]
# dtype: category
# Categories [2, interval[float64]]: [[-0.001, 25.0] < [25.0, 100.0]]
0s_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
6nguồn.
Nếu bạn muốn đếm số phần tử trong thùng, hãy sử dụng
print[pd.cut[s, 3]]
# a [-0.1, 33.333]
# b [-0.1, 33.333]
# c [-0.1, 33.333]
# d [-0.1, 33.333]
# e [-0.1, 33.333]
# f [-0.1, 33.333]
# g [33.333, 66.667]
# h [33.333, 66.667]
# i [33.333, 66.667]
# j [66.667, 100.0]
# k [66.667, 100.0]
# dtype: category
# Categories [3, interval[float64]]: [[-0.1, 33.333] < [33.333, 66.667] < [66.667, 100.0]]
print[pd.cut[s, 3, precision=1]]
# a [-0.1, 33.3]
# b [-0.1, 33.3]
# c [-0.1, 33.3]
# d [-0.1, 33.3]
# e [-0.1, 33.3]
# f [-0.1, 33.3]
# g [33.3, 66.7]
# h [33.3, 66.7]
# i [33.3, 66.7]
# j [66.7, 100.0]
# k [66.7, 100.0]
# dtype: category
# Categories [3, interval[float64]]: [[-0.1, 33.3] < [33.3, 66.7] < [66.7, 100.0]]
4s_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
7nguồn.
Thí dụ. dữ liệu Titanic
Sử dụng dữ liệu Titanic làm ví dụ. Bạn có thể tải xuống từ Kaggle
Nó cũng có sẵn ở đây
- titanic_train. csv
Một số cột bị loại trừ
s_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
8nguồn.
Bin the
print[pd.qcut[s, 2]]
# a [-0.001, 25.0]
# b [-0.001, 25.0]
# c [-0.001, 25.0]
# d [-0.001, 25.0]
# e [-0.001, 25.0]
# f [-0.001, 25.0]
# g [25.0, 100.0]
# h [25.0, 100.0]
# i [25.0, 100.0]
# j [25.0, 100.0]
# k [25.0, 100.0]
# dtype: category
# Categories [2, interval[float64]]: [[-0.001, 25.0] < [25.0, 100.0]]
2 cột với print[pd.cut[s, 4, labels=['small', 'medium', 'large', 'x-large']]]
# a small
# b small
# c small
# d small
# e small
# f small
# g medium
# h medium
# i large
# j x-large
# k x-large
# dtype: category
# Categories [4, object]: [small < medium < large < x-large]
0s_cut = pd.cut[s, 4]
print[s_cut]
# a [-0.1, 25.0]
# b [-0.1, 25.0]
# c [-0.1, 25.0]
# d [-0.1, 25.0]
# e [-0.1, 25.0]
# f [-0.1, 25.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.0]
# k [75.0, 100.0]
# dtype: category
# Categories [4, interval[float64]]: [[-0.1, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.0]]
print[type[s_cut]]
#
9nguồn.
Để thêm kết quả dưới dạng một cột mới vào bản gốc
print[pd.qcut[s, 2]]
# a [-0.001, 25.0]
# b [-0.001, 25.0]
# c [-0.001, 25.0]
# d [-0.001, 25.0]
# e [-0.001, 25.0]
# f [-0.001, 25.0]
# g [25.0, 100.0]
# h [25.0, 100.0]
# i [25.0, 100.0]
# j [25.0, 100.0]
# k [25.0, 100.0]
# dtype: category
# Categories [2, interval[float64]]: [[-0.001, 25.0] < [25.0, 100.0]]
4, hãy làm như sau. Để ghi đè lên một cột hiện có, chỉ cần đặt tên cột bên trái là tên cột hiện cóprint[pd.cut[s, [0, 10, 50, 100]]]
# a NaN
# b [0, 10]
# c [0, 10]
# d [0, 10]
# e [10, 50]
# f [10, 50]
# g [10, 50]
# h [10, 50]
# i [50, 100]
# j [50, 100]
# k [50, 100]
# dtype: category
# Categories [3, interval[int64]]: [[0, 10] < [10, 50] < [50, 100]]
0nguồn.
Lưu ý rằng, trong ví dụ này, quy trình tạo hộp được thực hiện ngay lập tức để thuận tiện, nhưng lý tưởng nhất là giá trị còn thiếu
print[pd.cut[s, 4, right=False]]
# a [0.0, 25.0]
# b [0.0, 25.0]
# c [0.0, 25.0]
# d [0.0, 25.0]
# e [0.0, 25.0]
# f [25.0, 50.0]
# g [25.0, 50.0]
# h [25.0, 50.0]
# i [50.0, 75.0]
# j [75.0, 100.1]
# k [75.0, 100.1]
# dtype: category
# Categories [4, interval[float64]]: [[0.0, 25.0] < [25.0, 50.0] < [50.0, 75.0] < [75.0, 100.1]]
3 phải được bổ sung trước khi tạo hộp