Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重命名类别并将缺少的类别添加到系列中_Python_Pandas_Dataframe - Fatal编程技术网

Python 重命名类别并将缺少的类别添加到系列中

Python 重命名类别并将缺少的类别添加到系列中,python,pandas,dataframe,Python,Pandas,Dataframe,我想重命名这些类别,并将缺少的类别添加到一个系列中 我的代码: codedCol = bdAu['Bordersite'] print 'pre:' print codedCol.head(10) codedCol = codedCol.astype('category') codedCol = codedCol.cat.set_categories(['a','b','c','d','e','f','g','h','i','j']) print 'post:' print codedCol.h

我想重命名这些类别,并将缺少的类别添加到一个系列中

我的代码:

codedCol = bdAu['Bordersite']
print 'pre:'
print codedCol.head(10)
codedCol = codedCol.astype('category')
codedCol = codedCol.cat.set_categories(['a','b','c','d','e','f','g','h','i','j'])
print 'post:'
print codedCol.head(10)
当我这样做的时候,结果是NaN

pre:
0    3
1    3
2    2
3    2
4    3
5    4
6    5
7    3
8    3
9    3
Name: Bordersite, dtype: int64
post:
0    NaN
1    NaN
2    NaN
3    NaN
4    NaN
5    NaN
6    NaN
7    NaN
8    NaN
9    NaN
dtype: category
Categories (10, object): [a, b, c, d, ..., g, h, i, j]
我做错了什么

谢谢
Kheeran

您已将类别设置为以下内容:
['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j']
codedCat
中列中的当前值与任何类别都不匹配。因此,它们被重新设置为
NaN
。为了进一步阅读,请考虑这个例子:


由于
“a”
不是一个类别,它将被重新设置为
NaN

您已将类别设置为以下内容:
['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j']
codedCat
中列中的当前值与任何类别都不匹配。因此,它们被重新设置为
NaN
。为了进一步阅读,请考虑这个例子:


由于
“a”
不是一个类别,它首先会被重新设置为
NaN
,或者创建
类别
,您可以使用
.astype('category')
,但是
类别
是从您的列中添加的,或者
类别
,其中定义了参数
类别

您可以使用:

codedCol = bdAu['Bordersite']
codedCol = pd.Series(pd.Categorical(codedCol, categories=[0,1,2,3,4,5,6,7,8,9]))
print (codedCol)
0    3
1    3
2    2
3    2
4    3
5    4
6    5
7    3
8    3
9    3
dtype: category
Categories (10, int64): [0, 1, 2, 3, ..., 6, 7, 8, 9]
然后,但类别中的项目数量必须相同,否则错误:

ValueError:新类别需要与旧类别具有相同数量的项目


首先或创建
分类
您可以使用
.astype('category')
,但是
分类
是从您的列中添加的,或者
分类
带有参数
分类
,其中定义了

您可以使用:

codedCol = bdAu['Bordersite']
codedCol = pd.Series(pd.Categorical(codedCol, categories=[0,1,2,3,4,5,6,7,8,9]))
print (codedCol)
0    3
1    3
2    2
3    2
4    3
5    4
6    5
7    3
8    3
9    3
dtype: category
Categories (10, int64): [0, 1, 2, 3, ..., 6, 7, 8, 9]
然后,但类别中的项目数量必须相同,否则错误:

ValueError:新类别需要与旧类别具有相同数量的项目


用于将类别添加到系列。

用于将类别添加到系列。

您的理想结果是什么?我添加了一个答案…如果有帮助请告诉我。您的理想结果是什么?我添加了一个答案…如果有帮助请告诉我。谢谢jezrael。这就是我要找的。乔西,谢谢你的解释。谢谢耶兹雷尔。这就是我要找的。乔西,谢谢你的解释。@jezrael编辑你的代码以便我可以删除否决票。@jezrael编辑你的代码以便我可以删除否决票。
codedCol = codedCol.cat.rename_categories(['a','b','c','d','e','f','g','h','i','j'])
print (codedCol)
0    d
1    d
2    c
3    c
4    d
5    e
6    f
7    d
8    d
9    d
dtype: category
Categories (10, object): [a, b, c, d, ..., g, h, i, j]