我有一个99列的数据框用于dx1-dx99& 99为px1-px99,一列为mort:
dx1 dx2 dx3 . dx99 px1 px2 . px99 mort
E10 I12 E10 N18 R18 0FY 0TY 0DN 0DN 1
E10 I12 I31 E44 N17 0FY 0TY 0FT 5A1 0
E10 I12 N17 T86 T86 0TY 0FY 0DT 0
I12 E10 N18 A04 0TY 0FY 0DT 0T7 1
E10 I12 E10 N18 Z99 0TY 0FY 0
E10 N18 Z76 0FY 0TY 04Q 0D1 1
E10 N18 Z99 N25 E78 0TY 0FY 0WP 0
我希望将所有值保留在dx-dx99& px-px99其中匹配行的值为mort = 1,否则将它们设置为零。之后计算剩余代码的出现频率。
我试过了:
dx = df.loc[:,'dx1':'dx99']
X1pr = df.loc[:,'px1':'px99']
dx = dx.fillna(0)
X1p = X1pr.fillna(0)
death = df.loc[:,'mort']
df1 = pd.concat([dx, X1p, death], axis=1)
N = len(df1.columns)
keep = df1.iloc[:,-(N-1):].isin(["1"]).values
df1.iloc[:,:N-1] = df1.iloc[:,:N-1].where(keep, 0)
X1d = df1.[df1.columns[0:N-1]]
mat = X1d.as_matrix(columns=None)
values, counts = np.unique(mat.astype(str), return_counts=True)
matrix = []
for v,c in zip(values, counts):
matrix.append( [v,c])
icd9_counted_d = pd.DataFrame(matrix, columns = ['ICD_code', 'DEATHS'])
我在DEATHS栏中一无所获。有什么想法吗?
答案 0 :(得分:1)
IIUC:
In [31]: x.loc[x.mort != 1, x.columns != 'mort'] = ''
In [32]: x
Out[32]:
dx1 dx2 dx3 dx4 dx99 px1 px2 px3 px99 mort
0 E10 I12 E10 N18 R18 0FY 0TY 0DN 0DN 1
1 0
2 0
3 I12 E10 N18 A04 NaN 0TY 0FY 0DT 0T7 1
4 0
5 E10 N18 Z76 NaN NaN 0FY 0TY 04Q 0D1 1
6 0