本文总结如何基于一列或多列中的值,得到新列。
Updated: 2022 / 10 / 5
Python | Pandas | 一对一或多对一映射匹配到新列
字典
参考这里 1
一对一映射
mapping
原始数据
# example
print(data)
'''
A Num ... B priority
0 Y 962 ... P Highest
... ... ... ... ... ...
556 t 84 ... F Medium
... ... ... ... ... ...
560 t 193 ... F Low
'''
自定义字典
基于单列中的值,提出自定义字典。此例中,有自定义字典1个,键为A列中的值,值为被映射到C列中相应的值。
# key, 'A'; value, 'C'; mapping 'A' values to 'C' values
A_to_C_Short = {
...
('Y', ...): 'P',
...
(..., 't'): 'Others'
} # len(A_to_C_Short.keys()) = 6
'''
m = 0
for n in A_to_C_Short.keys():
m += len(n)
# m = 26
# there are 26 values in total
'''
完整字典
完整键
键为A列中的值。
# 'A'
A = data.groupby('A').agg('sum').index.to_list() # len(A) = 49
'''
['A', 'B', ...]
'''
完整字典
键为A列中的值,值为经过字典映射后理应被映射到C列中相应的值。
# primary mapping: mapping 'A' items in 'A' col to get a A_to_C full dict
A_to_C_Full = {}
for i in A:
for j in A_to_C_Short:
if i in j:
A_to_C_Full[i] = A_to_C_Short[j]
'''
print(A_to_C_Full):
{..., 'Y': 'P', ..., 't': 'Others', ...}
print(len(A_to_C_Full))
26
'''
映射
经过完整字典将基于A列中的值映射到C列中相应的值。
# secondary mapping: mapping 'A' values in 'A' col to new values in 'C' col by A_to_C_Full dict
data['C'] = data['A'].map(A_to_C_Full).fillna(0)
'''
A Num ... C
0 Y 962 P
... ... ... ... ...
556 t 84 ... Others
... ... ... ... ...
560 t 193 ... Others
'''
多对一映射
apply
原始数据
# example
print(data)
'''
A Num ... B priority
0 Y 962 ... P Highest
... ... ... ... ... ...
556 t 84 ... F Medium
... ... ... ... ... ...
560 t 193 ... F Low
'''
自定义字典
基于多列中的值,提出自定义字典。此例中,有自定义字典2个,键为A或B列中的值,值为被映射到C列中相应的值。
字典A
键为A列中的值,值为被映射到C列中相应的值。
# key, 'A'; value, 'C'; mapping 'A' values to 'C' values
A_to_C_Short = {
...
('Y', ...): 'P',
...
(..., 't'): 'Others'
} # len(A_to_C_Short.keys()) = 6
'''
m = 0
for n in A_to_C_Short.keys():
m += len(n)
# m = 26
# there are totally 26 items in 6 keys
'''
字典B
键为B列中的值,值为被映射到C列中相应的值。
# key, 'B'; value, 'C'; mapping 'B' values to 'C' values
B_to_C_Short = {
'P': 'P',
('A', 'F', 'H', 'R', 'None', 'F'): 'N'
}
完整字典
完整键
键为A列与B列中的值组成的元组。
# 'A' & 'B' multi index in the format of tuple
MultiIndex = data.groupby(['A', 'B']).agg('sum').index.to_list() # <class 'list'>
'''
print(len(MultiIndex))
53
print(MultiIndex)
[('AR', 'F'), ('BW', 'P'), ('CW', 'P'), ...]
'''
完整字典
键为A列与B列中的值组成的元组,值为经过字典A和字典B映射后理应被映射到C列中相应的值。
# based on tuple values in 'A' col and 'B' col, and user define dicts,
# get a A & B col to C col mapping dict
MultiIndex_to_C_Full = {}
for i in MultiIndex: # i, e.g. ('AR', 'F')
for j in A_to_C_Short: # j, e.g. ('Y', ...)
for k in B_to_C_Short: # k, e.g. ('A', 'F', 'H', 'R', 'None', 'F')
if k == 'P':
if i[1] == k and i[0] in j:
MultiIndex_to_C_Full[i] = A_to_C_Short[j]
else:
if i[1] in k and i[0] not in j:
MultiIndex_to_C_Full[i] = B_to_C_Short[k]
'''
print(len(MultiIndex_to_C_Full))
53
pprint(MultiIndex_to_C_Full)
{('AR', 'F'): 'N',
...
('Y', 'P'): 'P',
'''
映射
经过完整字典将基于A和B列中的值映射到C列中相应的值。
data['C'] = data[['A', 'B']].apply(lambda x: MultiIndex_to_C_Full.get((x.A, x.B)), axis=1)
'''
A Num ... B C
...
3 Y 962 ... P P
...
566 t 193 ... F N
'''
函数
一对一映射
mapping
原始数据
表达式
参考这里 2
原始数据参考此处 3,简略图如下:
一对一映射
利用表达式,根据某一列值的范围,对另一列进行赋值。
比如,Choice
值大于 3
的 IP
,对应的 Node
值为 Top
;否则为 Medium
。
data = pd.read_excel('Mapping_RawData.xlsx', sheet_name='Sheet1')
data.loc[(data.Choice > 3), 'Node'] = 'Top'
data.loc[(data.Choice <= 3), 'Node'] = 'Medium'
得到的结果如下所示:
多对一映射
利用表达式,根据某几列值的范围,对另一列进行赋值。
比如,Choice
值小于 2
的在 127.0.0.7 ~ 127.0.0.12
中的 IP
,对应的 Node
值为 High
;否则为 Low
。
data = pd.read_excel('./Ex_Pandas.xlsx', sheet_name='Sheet1')
data.loc[(data.Choice < 2) & (data.IP.isin(['127.0.0.7', '127.0.0.8', '127.0.0.9', '127.0.0.10', '127.0.0.11', '127.0.0.12'])), 'Node'] = 'High'
data.loc[~((data.Choice < 2) & (data.IP.isin(['127.0.0.7', '127.0.0.8', '127.0.0.9', '127.0.0.10', '127.0.0.11', '127.0.0.12']))), 'Node'] = 'Low'
得到的结果如下所示: