简体   繁体   English

替换所有以数字python开头的内容

[英]Replace everything that starts with number python

I am working with ICD-9 codes for a data mining project using python and I am having trouble converting the specific codes into categories. 我正在为使用python的数据挖掘项目使用ICD-9代码,但无法将特定代码转换为类别。 For example, I am trying to change everything that's between 001 and 139 with 0, everything that's between 140 and 239 with 1, etc 例如,我试图用0更改介于001和139之间的所有内容,使用1更改介于140和239之间的所有内容,等等

This is what I have tried: 这是我尝试过的:

df = df.replace({'diag_1' : {'(1-139)' : 0, '(140-239)' : 1}})

You can use pd.cut to achieve this: 您可以使用pd.cut实现此目的:

In [175]:
df = pd.DataFrame({'value':np.random.randint(0,20,10)})
df

Out[175]:
   value
0     12
1      2
2     10
3      5
4     19
5      2
6      8
7     14
8     12
9     16

here we set bin intervals of (0-5) (5-15), (15-20): 在这里我们将bin间隔设置为(0-5)(5-15),(15-20):

In [183]:    
df['new_value'] = pd.cut(df['value'], bins=[0,5,15,20], labels=[0,1,2])
df

Out[183]:
   value new_value
0     12         1
1      2         0
2     10         1
3      5         0
4     19         2
5      2         0
6      8         1
7     14         1
8     12         1
9     16         2

I think in your case the following should work: 我认为在您的情况下,以下方法应该起作用:

df['diag_1']= pd.cut(df['diag_1'], [1,140,240] , labels=[1,2,3])

you can set the bins and labels dynamically using np.arange or similar 您可以使用np.arange或类似设置动态设置垃圾箱和标签

There is nothing wrong with an if-statement. 如果语句没有错。

newvalue = 1 if oldvalues <= 139 else 2

Apply this function as a lambda expression with map . 将此功能与map一起用作lambda表达式。

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

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