Pandas - 用名称ID替换数字字符串

sec*_*guy 4 python pandas

我在所有字符串的数据框中都有一个列,其中一些是TAG(机器/计算机),一些其他项目,其他是ID.我期待将ID的所有字符串更改为"ID"而不是数字字符串.

type(df.columnOne[1])
str 
Run Code Online (Sandbox Code Playgroud)

这就是我的df列的样子:

df
  columnOne
0 TAG
1 1115268
2 13452
3 system
4 TAG
5 355511
6 95221543
7 5124
8 111333544
9 TAG
10 local
11 434312
Run Code Online (Sandbox Code Playgroud)

期望的输出:

df
  columnOne
0 TAG
1 ID
2 ID
3 system
4 TAG
5 ID
6 ID
7 ID
8 ID
9 TAG
10 Local
11 ID
Run Code Online (Sandbox Code Playgroud)

我通常会做一些事情,如果它不等于TAG或系统或本地然后ID.但它总是随着名字而改变.

sac*_*cuL 5

如果我理解正确,你可以使用str.isnumeric:

df.loc[df.columnOne.str.isnumeric(),'columnOne'] = 'ID'

>>> df
   columnOne
0        TAG
1         ID
2         ID
3     system
4        TAG
5         ID
6         ID
7         ID
8         ID
9        TAG
10     local
11        ID
Run Code Online (Sandbox Code Playgroud)

  • 这只是考虑`ints` (2认同)