Python 在'上拆分||';熊猫系列
我想在| |Python 在'上拆分||';熊猫系列,python,pandas,dataframe,split,series,Python,Pandas,Dataframe,Split,Series,我想在| | ser=pd.Series(['there a guy || I will have a bite || no can do','I can do || more']) ser.str.split('||') **我应该得到输出为[['有人','我会咬一口','不行',['我能做','更多]] 但我明白了 0 [, t, h, e, r, e, s, , a, , g, u, y, , |, |... 1 [, I, , c, a, n, , d, o,
ser=pd.Series(['there a guy || I will have a bite || no can do','I can do || more'])
ser.str.split('||')
**我应该得到输出为[['有人','我会咬一口','不行',['我能做','更多]]
但我明白了
0 [, t, h, e, r, e, s, , a, , g, u, y, , |, |...
1 [, I, , c, a, n, , d, o, , |, |, , m, o, r...
dtype: object
Double
|
像regex一样处理,因此需要通过\
转义此值:
a = ser.str.split('\|\|')
print (a)
0 [there a guy , I will have a bite , no can do]
1 [I can do , more]
dtype: object
如果需要多个列:
>>> ser.str.split('\|\|',expand=True)
0 1 2
0 there a guy I will have a bite no can do
1 I can do more None
>>>
为了避免转义,我建议改用字符类:
ser.str.split(r'[|]{2}')
0 [there a guy , I will have a bite , no can do]
1 [I can do , more]
dtype: object
或者,没有必要逃避自己,因为re.escape
可以帮你
import re
ser.str.split(re.escape('||'))
0 [there a guy , I will have a bite , no can do]
1 [I can do , more]