如何用唯一ID替换Python Pandas表文本值?

Kin*_*gua 3 python python-3.x pandas

我正在使用Pandas以这种格式读取文件:

fp = pandas.read_table("Measurements.txt")
fp.head()

"Aaron", 3, 5, 7  
"Aaron", 3, 6, 9  
"Aaron", 3, 6, 10 
"Brave", 4, 6, 0 
"Brave", 3, 6, 1
Run Code Online (Sandbox Code Playgroud)

我想用唯一的ID替换每个名称,因此输出如下:

"1", 3, 5, 7 
"1", 3, 6, 9 
"1", 3, 6, 10 
"2", 4, 6, 0 
"2", 3, 6, 1
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

谢谢!

Max*_*axU 6

我会使用分类 dtype:

In [97]: x['ID'] = x.name.astype('category').cat.rename_categories(range(1, x.name.nunique()+1))

In [98]: x
Out[98]:
    name  v1  v2  v3 ID
0  Aaron   3   5   7  1
1  Aaron   3   6   9  1
2  Aaron   3   6  10  1
3  Brave   4   6   0  2
4  Brave   3   6   1  2
Run Code Online (Sandbox Code Playgroud)

如果您需要字符串ID而不是数字ID,您可以使用:

x.name.astype('category').cat.rename_categories([str(x) for x in range(1,x.name.nunique()+1)])
Run Code Online (Sandbox Code Playgroud)

或者,正如@MedAli在他的回答中提到的,使用factorize()方法 - 演示:

In [141]: x['cat'] = pd.Categorical((pd.factorize(x.name)[0] + 1).astype(str))

In [142]: x
Out[142]:
    name  v1  v2  v3 ID cat
0  Aaron   3   5   7  1   1
1  Aaron   3   6   9  1   1
2  Aaron   3   6  10  1   1
3  Brave   4   6   0  2   2
4  Brave   3   6   1  2   2

In [143]: x.dtypes
Out[143]:
name      object
v1         int64
v2         int64
v3         int64
ID      category
cat     category
dtype: object

In [144]: x['cat'].cat.categories
Out[144]: Index(['1', '2'], dtype='object')
Run Code Online (Sandbox Code Playgroud)

或者将类别作为整数:

In [154]: x['cat'] = pd.Categorical((pd.factorize(x.name)[0] + 1))

In [155]: x
Out[155]:
    name  v1  v2  v3 ID cat
0  Aaron   3   5   7  1   1
1  Aaron   3   6   9  1   1
2  Aaron   3   6  10  1   1
3  Brave   4   6   0  2   2
4  Brave   3   6   1  2   2

In [156]: x['cat'].cat.categories
Out[156]: Int64Index([1, 2], dtype='int64')
Run Code Online (Sandbox Code Playgroud)

说明:

In [99]: x.name.astype('category')
Out[99]:
0    Aaron
1    Aaron
2    Aaron
3    Brave
4    Brave
Name: name, dtype: category
Categories (2, object): [Aaron, Brave]

In [100]: x.name.astype('category').cat.categories
Out[100]: Index(['Aaron', 'Brave'], dtype='object')

In [101]: x.name.astype('category').cat.rename_categories([1,2])
Out[101]:
0    1
1    1
2    1
3    2
4    2
dtype: category
Categories (2, int64): [1, 2]
Run Code Online (Sandbox Code Playgroud)

factorize()方法的解释:

In [157]: (pd.factorize(x.name)[0] + 1)
Out[157]: array([1, 1, 1, 2, 2])

In [158]: pd.Categorical((pd.factorize(x.name)[0] + 1))
Out[158]:
[1, 1, 1, 2, 2]
Categories (2, int64): [1, 2]
Run Code Online (Sandbox Code Playgroud)


Moh*_*OUI 5

你可以通过一个简单的字典映射来做到这一点。比如说你的数据是这样的:

col1, col2, col3, col4
"Aaron", 3, 5, 7  
"Aaron", 3, 6, 9  
"Aaron", 3, 6, 10 
"Brave", 4, 6, 0 
"Brave", 3, 6, 1
Run Code Online (Sandbox Code Playgroud)

然后简单地做

myDict = {"Aaron":"1", "Brave":"2"}
fp["col1"] = fp["col1"].map(myDict)
Run Code Online (Sandbox Code Playgroud)

如果您不想构建字典,请使用pandas.factorize它将负责从 0 开始为您编码列。您可以在此处找到有关如何使用它的示例。