【发布时间】:2020-07-10 11:56:33
【问题描述】:
我有一个包含不同列的数据框,例如:
1) 自定义手机号
2) 自定义家庭电话
3) 定制 nextkin 手机
4) 自定义传真
5) 客户ID
在我的输出数据框中,我希望有如下列:
1) 客户 ID
2) 客户电话 1
3) 客户电话 2
4) 客户电话 3
5) 客户电话 4
输入输出电话号码的映射如下(但也有优先级逻辑):
cust phone 1 = cust mobile phone no
cust phone 2 = cust home phone
cust phone 3 = cust nextkin phone
cust phone 4 = cust fax
请注意,输入数据框中的任何这些都可能是空白的。优先级逻辑表示,如果一个为空,则应将下一个可用电话号码分配给该电话列。因此,如果 cust phone 2 为空白但 cust phone 3 可用,则应为 cust phone 2 分配该值,依此类推。此外,客户电话 1 到客户电话 4 都应该是唯一的(没有重复)。
由于数据框很大,因此不能对行进行迭代。
这是一个示例数据框:
df = pd.DataFrame({'cust mobile no': ['1', '2', '3'],
'cust home phone': [np.nan, '2', 'x'],
'cust nextkin phone': ['1', '2', 'g'],
'cust fax': [np.nan, '4', '5'],
'cust id': ['001', '002', '003']})
cust mobile no cust home phone cust nextkin phone cust fax cust id
0 1 NaN 1 NaN 001
1 2 2 2 4 002
2 3 x g 5 003
预期输出:
cust id cust phone 1 cust phone 2 cust phone 3 cust phone 4
0 001 1 NaN NaN NaN
1 002 2 4 NaN NaN
2 003 3 x g 5
【问题讨论】:
-
也许this 的回答会对你有所帮助。
-
感谢链接,但我找不到链接与我的问题之间的任何关系