python-3.x - python中矩阵中的多个字符替换

标签 python-3.x pandas

我仍在学习Python,我有一个向量,它属于一个相当大的矩阵,并且该向量中的条目是对象类型。它们是('< 1 年'、'1 年'、'2 年'等等) 我想分别改成0、1、2、3。我编写了以下行,这些行有效,但必须有更简单的解决方案,不需要具有 10 个条件的循环:

import numpy as np
import pandas as pd

data_file = pd.read_csv('loan.csv')

emp_length=data_file.emp_length
emp_len=[]
for i in range(len(emp_length)):
   if emp_length[i]=='< 1 year':
       emp_len.append(0)
   elif emp_length[i]=='1 year':
       emp_len.append(1)
   elif emp_length[i]=='2 years':
       emp_len.append(2)
   elif emp_length[i]=='3 years':
       emp_len.append(3)
   elif emp_length[i]=='4 years':
       emp_len.append(4)
   elif emp_length[i]=='5 years':
       emp_len.append(5)
   elif emp_length[i]=='6 years':
       emp_len.append(6)
   elif emp_length[i]=='7 years':
       emp_len.append(7)
   elif emp_length[i]=='8 years':
       emp_len.append(8)
   elif emp_length[i]=='9 years':
       emp_len.append(9)
   elif emp_length[i]=='10+ years':
       emp_len.append(10)
   else:
       emp_len.append(0)

我不需要创建新的向量,但这是我自己想出的解决方案。如果无论如何可以替换同一向量中的这些条目,那就更好了。感谢您的建议和帮助

最佳答案

考虑数据帧df

np.random.seed([3,1415])
df = pd.DataFrame(dict(emp_length=np.random.choice(list(m.keys()), 20)))
print(df)

   emp_length
0    < 1 year
1     2 years
2   10+ years
3   10+ years
4     7 years
5   10+ years
6     3 years
7     8 years
8     7 years
9   10+ years
10   < 1 year
11    6 years
12    8 years
13    6 years
14   < 1 year
15  10+ years
16    2 years
17   < 1 year
18    4 years
19    9 years

您可以使用map或用字典replace

m = {
   '< 1 year': 0,
   '1 year': 1,
   '2 years': 2,
   '3 years': 3,
   '4 years': 4,
   '5 years': 5,
   '6 years': 6,
   '7 years': 7,
   '8 years': 8,
   '9 years': 9,
   '10+ years': 10
}

data_file.emp_length.map(m)
# or equivalently
# data_file.emp_length.replace(m)

0      0
1      2
2     10
3     10
4      7
5     10
6      3
7      8
8      7
9     10
10     0
11     6
12     8
13     6
14     0
15    10
16     2
17     0
18     4
19     9
Name: emp_length, dtype: int64

您还可以使用分类类型

cats = ['< 1 year', '1 year', '2 years', '3 years', '4 years', '5 years', '6 years', '7 years', '8 years', '9 years', '10+ years']
c = df.emp_length.astype('category', categories=cats, ordered=True)
print(c)

0      < 1 year
1       2 years
2     10+ years
3     10+ years
4       7 years
5     10+ years
6       3 years
7       8 years
8       7 years
9     10+ years
10     < 1 year
11      6 years
12      8 years
13      6 years
14     < 1 year
15    10+ years
16      2 years
17     < 1 year
18      4 years
19      9 years
Name: emp_length, dtype: category
Categories (11, object): [< 1 year < 1 year < 2 years < 3 years ... 7 years < 8 years < 9 years < 10+ years]

然后您可以使用以下命令访问映射的整数

c.cat.codes

0      0
1      2
2     10
3     10
4      7
5     10
6      3
7      8
8      7
9     10
10     0
11     6
12     8
13     6
14     0
15    10
16     2
17     0
18     4
19     9
dtype: int8

关于python-3.x - python中矩阵中的多个字符替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244043/

相关文章:

python-3.x - 具有随机效应的多级回归中变量的解释

python - Pandas,验证数据,检查所有组的长度是否相同

python - 从python中的kibana查询?

python - 查找列表中出现次数最多的数字

python - 模块未找到错误 : No module named 'tensorflow.python.training'

python-3.x - 在 pandas.DataFrame 的多列上使用 numpy.unique

python - 将列添加到 pandas 数据框中,从其他列中的列表中获取值

python - 检测 2 个数据帧中的重叠值

python - 有没有办法将两个列表与 xarray 数据变量中的相应信息连接起来?

pandas - "None of [Int64Index , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,\n ...... dtype=' int64 ')] are in the [columns]"