pandas 对数据框中列中的数据进行分类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38936854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:49:03  来源:igfitidea点击:

Categorize Data in a column in dataframe

pythonpandasmachine-learningdata-analysis

提问by Nathaniel Babalola

I have a column of numbers in my dataframe, i want to categorize these numbers into e.g high , low, excluded. How do i accomplish that. I am clueless , i have tried looking at the cut function and category datatype.

我的数据框中有一列数字,我想将这些数字分类为例如高、低、排除。我如何做到这一点。我一无所知,我曾尝试查看剪切函数和类别数据类型。

回答by ptrj

A short example with pd.cut.

一个简短的例子pd.cut

Let's start with some data frame:

让我们从一些数据框开始:

df = pd.DataFrame({'A': [0, 8, 2, 5, 9, 15, 1]})

and, say, we want to assign the numbers to the following categories: 'low'if a number is in the interval [0, 2], 'mid'for (2, 8], 'high'for (8, 10], and we exclude numbers above 10 (or below 0).

并且,比如说,我们想要将数字分配到以下类别:'low'如果一个数字在区间[0, 2]'mid'for (2, 8]'high'for 中(8, 10],我们排除大于 10(或小于 0)的数字。

Thus, we have 3 bins with edges: 0, 2, 8, 10. Now, we can use cutas follows:

因此,我们有 3 个带边的 bin:0、2、8、10。现在,我们可以使用cut如下:

pd.cut(df['A'], bins=[0, 2, 8, 10], include_lowest=True)
Out[33]: 
0     [0, 2]
1     (2, 8]
2     [0, 2]
3     (2, 8]
4    (8, 10]
5        NaN
6     [0, 2]
Name: A, dtype: category
Categories (3, object): [[0, 2] < (2, 8] < (8, 10]]

The argument include_lowest=Trueincludes the left end of the first interval. (If you want intervals open on the right, then use right=False.)

参数include_lowest=True包括第一个间隔的左端。(如果您希望在右侧打开间隔,请使用right=False。)

The intervals are probably not the best names for the categories. So, let's use names: low/mid/high:

间隔可能不是类别的最佳名称。所以,让我们使用名称low/mid/high

pd.cut(df['A'], bins=[0, 2, 8, 10], include_lowest=True, labels=['low', 'mid', 'high'])
Out[34]: 
0     low
1     mid
2     low
3     mid
4    high
5     NaN
6     low
Name: A, dtype: category
Categories (3, object): [low < mid < high]

The excluded number 15 gets a "category" NaN. If you prefer a more meaningful name, probably the simplest solution (there're other ways to deal with NaN's) is to add another bin and a category name, for example:

排除的数字 15 获得一个“类别” NaN。如果您更喜欢更有意义的名称,可能最简单的解决方案(还有其他方法可以处理 NaN)是添加另一个 bin 和类别名称,例如:

pd.cut(df['A'], bins=[0, 2, 8, 10, 1000], include_lowest=True, labels=['low', 'mid', 'high', 'excluded'])
Out[35]: 
0         low
1         mid
2         low
3         mid
4        high
5    excluded
6         low
Name: A, dtype: category
Categories (4, object): [low < mid < high < excluded]

回答by draco_alpine

This question is pretty broad, but a good place to start might be this page in the documentation:

这个问题非常广泛,但一个很好的起点可能是文档中的这个页面:

http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing

http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing

Or you could look into numpy.where

或者你可以看看 numpy.where

    import numpy as np
    df['is_high'] = np.where(df.['column_of_interest'] > 5 ,1,0)