当Na存在时,用等价字母替换矩阵中的所有数字 [英] substitute all numbers in a matrix with equivalent letters when Na exists

查看:114
本文介绍了当Na存在时,用等价字母替换矩阵中的所有数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个巨大的矩阵,其元素为1到15范围内的数字.我想将矩阵转换为元素为字母的那个矩阵,以使1变为"a",2变为"b",依此类推. .最后,我想合并每一行并创建一个序列.举一个简单的例子:

There is a huge matrix whose elements are numbers in the range of 1 to 15. I want to transform the matrix to the one whose elements be letters such that 1 becomes "a", 2 becomes "b", and so on. Finally I want to merge each row and create a sequence of it. As a simple example:

import pandas as pd
import numpy as np, numpy.random
numpy.random.seed(1)
A = pd.DataFrame (np.random.randint(1,16,10).reshape(2,5)) 
A.iloc[1,4]= np.NAN
A
#   0   1   2   3   4
#0  6   12  13  9   10.0
#1  12  6   1   1   NaN

如果数据集中没有Na,我将使用以下代码:

If there were no Na in the dataset, I would use this code:

pd.DataFrame(list(map(''.join, A.applymap(lambda n: chr(n + 96)).as_matrix())))

在这里,它会显示此错误:

Here, it gives this error:

TypeError: ('integer argument expected, got float', 'occurred at index 4')

预期输出为:

    0
0   flmij
1   lfaa

第一行应包含5个元素,第二行应包含4个元素.

The first row should have 5 elements and the second one should have 4 elements.

推荐答案

sum中使用if-else条件:

df = pd.DataFrame(A.applymap(lambda n: chr(int(n) + 96) if pd.notnull(n) else '')
                   .values.sum(axis=1))
print (df)
       0
0  flmij
1   lfaa

详细信息:

print (A.applymap(lambda n: chr(int(n) + 96) if pd.notnull(n) else ''))
   0  1  2  3  4
0  f  l  m  i  j
1  l  f  a  a   

print (A.applymap(lambda n: chr(int(n) + 96) if pd.notnull(n) else '').values)
[['f' 'l' 'm' 'i' 'j']
 ['l' 'f' 'a' 'a' '']]

print (A.applymap(lambda n: chr(int(n) + 96) if pd.notnull(n) else '').values.sum(axis=1))
['flmij' 'lfaa']

另一种解决方案:

print (A.stack().astype(int).add(96).apply(chr).sum(level=0))
0    flmij
1     lfaa
dtype: object

详细信息:

重塑为Series:

print (A.stack())
0  0     6.0
   1    12.0
   2    13.0
   3     9.0
   4    10.0
1  0    12.0
   1     6.0
   2     1.0
   3     1.0
dtype: float64

转换为integer s:

print (A.stack().astype(int))
0  0     6
   1    12
   2    13
   3     9
   4    10
1  0    12
   1     6
   2     1
   3     1
dtype: int32

添加号码:

print (A.stack().astype(int).add(96))
0  0    102
   1    108
   2    109
   3    105
   4    106
1  0    108
   1    102
   2     97
   3     97
dtype: int32

转换为letter s:

print (A.stack().astype(int).add(96).apply(chr))
0  0    f
   1    l
   2    m
   3    i
   4    j
1  0    l
   1    f
   2    a
   3    a
dtype: object

第一级MultiIndex的总和:

print (A.stack().astype(int).add(96).apply(chr).sum(level=0))
0    flmij
1     lfaa
dtype: object

这篇关于当Na存在时,用等价字母替换矩阵中的所有数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆