Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在'上拆分||';熊猫系列_Python_Pandas_Dataframe_Split_Series - Fatal编程技术网

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]