Polars中使用正则处理字符串分隔与替换的技术问题
Polars字符串拆分并添加逗号解决方案
基础场景:equip列拆分
原始DataFrame
import polars as pl pl.Config.set_fmt_str_lengths(100) df = pl.DataFrame({ "equip": [ 'AmuletsMedals', 'Guns, CrossbowsOff-Hands', 'Melee WeaponsShieldsOff-Hands', 'All Armor', 'Chest Armor', 'Shields', 'All WeaponsShieldsOff-Hands' ] })
预期结果
expected = pl.DataFrame({ "equip": [ "Amulets, Medals", "Guns, Crossbows, Off-Hands", "Melee Weapons, Shields, Off-Hands", "All Armor", "Chest Armor", "Shields", "All Weapons, Shields, Off-Hands" ] })
问题分析
直接使用str.replace("[a-z][A-Z]", ", ")会截断匹配到的首尾字符,传入表达式的方式也无法生效。
解决方案
使用零宽断言匹配小写字母与大写字母之间的位置,插入逗号空格,不会修改原有字符:
result = df.with_columns( pl.col("equip").str.replace(r"(?<=[a-z])(?=[A-Z])", ", ") ) print(result)
输出结果与预期完全一致。
复杂场景:attributes列拆分
原始DataFrame
df2 = pl.DataFrame({"attributes": ["+10% Aether Damage+30 Defensive Ability16% Aether Resistance6% Less Damage from Aetherials6% Less Damage from Aether Corruptions", "4-6 Aether Damage+25% Aether Damage10% Physical Damage converted to Aether DamageAether Tendril (Granted by Item)", "2-8 Lightning Damage+25% Lightning Damage+25% Electrocute Damage10% Physical Damage converted to Lightning DamageEmpowered Lightning Nova (Granted by Item)", "+10 Health Regenerated per Second+24 Armor20% Poison & Acid Resistance", "+22 Defensive Ability10% Chance to Avoid Projectiles+18 Armor", "+15 Physique+10% Shield Block ChanceShield Slam (Granted by Item)", "+10% Chaos Damage+30 Defensive Ability16% Chaos Resistance6% Less Damage from Chthonics"]})
解决方案
需要匹配多种拆分位置:小写字母后跟大写字母、字母后跟数字/加号,用零宽断言组合正则模式:
result2 = df2.with_columns( pl.col("attributes").str.replace( r"(?<=[a-z])(?=[A-Z])|(?<=[a-zA-Z])(?=[+0-9])", ", " ) ) print(result2)
拆分后每个属性都会被逗号空格分隔,符合需求。
内容的提问来源于stack exchange,提问作者Victor Reial
相关产品推荐
相关产品推荐

