# Pandas cut 函式

Suraj Joshi 2023年1月30日

`pandas.cut()` 函式可以將給定的資料分佈到範圍內，範圍也可稱為 `bins`

``````import pandas as pd

df = pd.DataFrame(
{
"Name": ["Anish", "Birat", "Chirag", "Kabin", "Sachin"],
"Age": [23, 34, 38, 45, 27],
"Score": [316, 322, 332, 330, 325],
}
)

print(df)
``````

``````     Name  Age  Score
0   Anish   23    316
1   Birat   34    322
2  Chirag   38    332
3   Kabin   45    330
4  Sachin   27    325
``````

## `pandas.cut()` 函式語法

``````pandas.cut(
x,
bins,
right=True,
labels=None,
retbins=False,
precision=3,
include_lowest=False,
duplicates="raise",
ordered=True,
)
``````

### 引數

`x` 給定陣列
`bins` 資料分類的標準
`right` 布林型。如果為 True，也包括 `bins` 最右邊的數字。
`labels` 陣列。`bins` 的標籤。
`retbins` 布林型。如果是 `True`，則返回 `bins`
`precision` 整數型。儲存和展示 `bins` 的精度
`ordered` 布林型。如果為 `True`，則將對結果的標籤進行排序。

## 示例：使用 `pandas.cut()` 方法將 DataFrame 的列值分佈到 bin 中去

``````import pandas as pd

df = pd.DataFrame(
{
"Name": ["Anish", "Birat", "Chirag", "Kabin", "Sachin"],
"Age": [23, 34, 38, 45, 27],
"Score": [316, 322, 332, 330, 325],
}
)

print("Initial DataFrame:")
print(df, "\n")

df["Age-Range"] = pd.cut(x=df["Age"], bins=[20, 30, 40, 50])

print("DataFrame with Age-Range:")
print(df)
``````

``````Initial DataFrame:
Name  Age  Score
0   Anish   23    316
1   Birat   34    322
2  Chirag   38    332
3   Kabin   45    330
4  Sachin   27    325

DataFrame with Age-Range:
Name  Age  Score Age-Range
0   Anish   23    316  (20, 30]
1   Birat   34    322  (30, 40]
2  Chirag   38    332  (30, 40]
3   Kabin   45    330  (40, 50]
4  Sachin   27    325  (20, 30]
``````

## 示例：使用 `pandas.cut()` 方法將數值分佈到不同的區域，併為每個區域分配一個標籤

``````import pandas as pd

df = pd.DataFrame(
{
"Name": ["Anish", "Birat", "Chirag", "Kabin", "Sachin"],
"Age": [23, 34, 38, 45, 27],
"Score": [316, 322, 332, 330, 325],
}
)

print("Initial DataFrame:")
print(df, "\n")

bin_labels = labels = ["21 to 30", "31 to 40", "41 to 50"]
df["Age-Range"] = pd.cut(x=df["Age"], bins=[20, 30, 40, 50], labels=bin_labels)

print("DataFrame with Age-Range:")
print(df)
``````

``````Initial DataFrame:
Name  Age  Score
0   Anish   23    316
1   Birat   34    322
2  Chirag   38    332
3   Kabin   45    330
4  Sachin   27    325

DataFrame with Age-Range:
Name  Age  Score Age-Range
0   Anish   23    316  21 to 30
1   Birat   34    322  31 to 40
2  Chirag   38    332  31 to 40
3   Kabin   45    330  41 to 50
4  Sachin   27    325  21 to 30
``````

## 示例：在 `pandas.cut()` 方法中設定 `retbins=True` 來返回 bin 值

``````import pandas as pd

df = pd.DataFrame(
{
"Name": ["Anish", "Birat", "Chirag", "Kabin", "Sachin"],
"Age": [23, 34, 38, 45, 27],
"Score": [316, 322, 332, 330, 325],
}
)

print("Initial DataFrame:")
print(df, "\n")

bin_labels = labels = ["21 to 30", "31 to 40", "41 to 50"]
df["Age-Range"], bin_values = pd.cut(
x=df["Age"], bins=[20, 30, 40, 50], labels=bin_labels, retbins=True
)

print("DataFrame with Age-Range:")
print(df, "\n")

print("The bin values are:")
print(bin_values)
``````

``````Initial DataFrame:
Name  Age  Score
0   Anish   23    316
1   Birat   34    322
2  Chirag   38    332
3   Kabin   45    330
4  Sachin   27    325

DataFrame with Age-Range:
Name  Age  Score Age-Range
0   Anish   23    316  21 to 30
1   Birat   34    322  31 to 40
2  Chirag   38    332  31 to 40
3   Kabin   45    330  41 to 50
4  Sachin   27    325  21 to 30

The bin values are:
[20 30 40 50]
``````

Suraj Joshi is a backend software engineer at Matrice.ai.