需求:有时候我们想把一串带有中英文的字符分成中文和英文部分
具体思路:使用中英文正则识别,用re.findall找出来
中文识别编码:[\u4e00-\u9fa5]
英文识别编码:[a-zA-Z]
代码实现
import re
import pandas as pd
# 中英文编码
en = re.compile(r'[a-zA-Z]')
cn = re.compile(r'[\u4e00-\u9fa5]')
# 提取中英文
for i in data.index:
data.loc[i,'cn_text'] = "".join(cn.findall(data.loc[i,'brand']))
data.loc[i,'en_text'] = "".join(en.findall(data.loc[i,'brand']))
英文大小写
data['en_lower'] = data.en_text.str.lower()
data['en_upper'] = data.en_text.str.upper()