gpt4 book ai didi

python - Pandas 用标签切割多列?

转载 作者:行者123 更新时间:2023-12-04 08:16:09 25 4
gpt4 key购买 nike

我想剪切并标记多列中的值。基本上是这样的:

df[numericColumn] = pd.cut(df[numericColumn], 3, labels=["small", "medium", "big"])

我找到了这个代码(有效)但是没有做我想要的标签,当我用标签替换 bin 时它出错并说我需要包含一个 bin

df = pd.DataFrame(np.random.rand(10,4))
df.apply(pd.cut, bins=[0,0.5,1])

有没有办法在 pandas 中切割标签多列?

最佳答案

所以把它拆开一点来展示发生了什么。您会看到您可以设置分箱,然后逐行进行切割,然后将切割结果更改回您要使用的类别名称。

# set up a dataframe
df = pd.DataFrame(np.random.rand(10,4))
df.columns = ['col1','col2','col3','col4']

# set up the intervals
bins = pd.IntervalIndex.from_tuples([(0, 0.33), (0.33, 0.66), (0.66, 1)])

# do the cut on col1 for example
x = pd.cut(df["col1"].to_list(),bins)

# change the name of the catagories
x.categories = ['small','medium','large']
# put it back
df['col1'] = x

给予

    col1    col2        col3        col4
0 large 0.589432 0.545828 0.257144
1 medium 0.625025 0.087607 0.548300
2 small 0.538186 0.057027 0.023201
3 medium 0.686324 0.027694 0.819753
4 medium 0.850623 0.977317 0.782361
5 large 0.375888 0.209709 0.903763
6 medium 0.094957 0.583052 0.534926
7 large 0.101968 0.863916 0.929300
8 large 0.612073 0.005553 0.723863
9 large 0.814393 0.327281 0.463976

如果你想遍历整个数据框

# set up a dataframe
df = pd.DataFrame(np.random.rand(10,4))
df.columns = ['col1','col2','col3','col4']

# set up the intervals
bins = pd.IntervalIndex.from_tuples([(0, 0.33), (0.33, 0.66), (0.66, 1)])
names = ['small','medium','large']

for col in df.columns:
x = pd.cut(df[col].to_list(),bins)
x.categories = names
df[col] = x

关于python - Pandas 用标签切割多列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65695647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com