简体   繁体   English

替换列 dataframe python 中的数值

[英]replace numeric values in column dataframe python

Im new using python.我是使用 python 的新手。 I have a dataframe with 3 columns (id, name, description) , My dataframe contains as example these rows ('id1', 'paul', 'tmp123'), ('id2', 'laura', 'vi34jay') .我有一个 dataframe 有 3 列(id, name, description) ,我的dataframe包含例如这些行('id1', 'paul', 'tmp123'), ('id2', 'laura', 'vi34jay') I want to replace the numeric characters of my column description by "TT" .我想用"TT"替换列描述的数字字符。

expected output预计 output

('id1', 'paul', 'tmpTTT'), ('id2', 'laura', 'viTTjay')

Does anyone knows how to do?有谁知道该怎么做? Thank you谢谢

Use Series.replace with \d for match digit:使用Series.replace\d匹配数字:

df = pd.DataFrame({'description': ['tmp123', 'vi34jay']})

df['description'] = df['description'].replace('\d', 'T', regex=True)
print(df)
  description
0      tmpTTT
1     viTTjay

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

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