我有一个非常庞大的excel数据集,包含多个IF条件的多列。其中一列具有以下公式:
=IF(A2532="Merlin",IF(LEFT(D2532,8)="RMZ.JPWK",RIGHT(D2532,LEN(D2532)-8),IF(LEFT(D2532,6)="RMZ.JP",RIGHT(D2532,LEN(D2532)-6),IF(LEFT(D2532,4)="RMZ.",RIGHT(D2532,LEN(D2532)-4),D2532))),IF(AND(A2532<>"Merlin",LEFT(D2532,4)="RMZ."),"",D2532))
数据帧示例如下:
#Country Code
=================
Merlin 010566
Merlin RMZ.JP828802
Merlin RMZ.JP828804
Merlin RMZ.JP828806
Merlin RMZ.JPNS9002
上述条件很容易在excel中实现。条件的要点是:
If Country is Merlin, then:
If first 8 characters of D2532 is RMZ.JPWK, then write first 8 characters of D2532
else if first 6 characters of D2532 is RMZ.JP, then write first 6 characters of D2532
else if first 4 characters of D2532 is RMZ., then write first 4 characters of D2532
If country is not Merlin then:
if first 4 characters of D2532 is RMZ., then write 'NAN'
预期输出数据帧:
#Country Code Result
================================
Merlin RMZ.JPWK821517 821517
Merlin RMZ.JPWK14202 14202
Merlin RMZ.JPWK14324 14324
Merlin RMZ.JPWK4003175001 4003175001
Merlin RMZ.JP828802 828802
Merlin RMZ.JP828804 828804
Merlin RMZ.JP828806 828806
Merlin RMZ.02029182001 02029182001
Merlin RMZ.02031790001 02031790001
Merlin RMZ.02519593001 02519593001
Sorex RMZ.02519593001 NAN
我可以通过创建满足每个条件的多个列,然后使用if else循环来编写解决方案,但是由于数据集非常庞大,运行起来需要很长时间
它还增加了我清理数据帧的工作,因为为每个条件创建了多个列,而实际上并不是每次都进行计算
请注意,上面的数据帧只是一小部分,实际数据库包含100个列和10万行
我想要的解决方案是:
-2.消除If循环的使用
你不再用Excel写公式了,所以不要把同样的心态带到工作中去。你的实际问题有两个方面:
Code
列末尾的数字代码Result
列Regex是第一个任务的好工具。第二个可以通过数据帧切片实现:
您的示例输出和伪条件没有任何意义。你写的东西完全不是样本中的结果
简单的答案是使用csv,使用python遍历它并保存您要查找的结果。忘了把它翻译成excel吧
您可以将csv重新导入excel
只要写一个CSV解析器就可以了
-每次找到新行字符时,您就知道它是新行。
-每次找到要查找的行时,都会存储整行或将其写入新文件
使用正则表达式
消除if循环的使用?你不能。唯一知道元素匹配是否使用条件匹配的方法,以及通过一组数据的唯一方法是通过它进行迭代
相关问题 更多 >
编程相关推荐