简体   繁体   English

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

[英]Pandas - Replace number-strings with the name ID

I have a column in my dataframe of all Strings some of them are TAG(machines/computers), some other items, and the others are ID's. 我在所有字符串的数据框中都有一个列,其中一些是TAG(机器/计算机),一些其他项目,其他是ID。 I am looking to change all the strings that are ID's to "ID" instead of the number-string. 我期待将ID的所有字符串更改为“ID”而不是数字字符串。

type(df.columnOne[1])
str 

This is what my df column looks like: 这就是我的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

Desired output: 期望的输出:

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

I would normally do something where if it doesn't equal TAG or system or Local then ID. 我通常会做一些事情,如果它不等于TAG或系统或本地然后ID。 But it is always changing with names. 但它总是随着名字而改变。

If I understand correctly, you can use str.isnumeric : 如果我理解正确,您可以使用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

Try replace 尝试更换

df.columnOne = df.columnOne.str.replace('\d+', 'ID')

0        TAG
1         ID
2         ID
3     system
4        TAG
5         ID
6         ID
7         ID
8         ID
9        TAG
10     local
11        ID

As RafaelC mentioned in the comment , if contain float 正如RafaelC在评论中提到的,如果包含浮点数

df.loc[pd.to_numeric(df.columnOne,errors='coerce').notna(),'columnOne']='ID'
df
Out[536]: 
   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

Solution using apply: 解决方案使用申请
(just for completeness, str.replace and str.isnumeric solutions are much more simple) (只是为了保持完整性, str.replacestr.isnumeric方案非常简单)

df = pd.DataFrame({'columnOne': ['TAG', 
                                 '1111', 
                                 'system']})

def ids_replace(x):
    try:
        int(x)
        return 'ID'
    except ValueError:
        return x

print(df.apply(ids_replace, axis=1))

> columnOne
0   TAG
1   ID
2   system

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM