roi*_*uez 5 python regex pandas
示例代码:
In [1]: import pandas as pd
In [2]: serie = pd.Series(['this#is#a#test', 'another#test'])
In [3]: serie.str.split('#', expand=True)
Out[3]:
0 1 2 3
0 this is a test
1 another test None None
Run Code Online (Sandbox Code Playgroud)
是否可以在不剥离分割条件字符串的情况下进行分割?上面的输出将是:
Out[3]:
0 1 2 3
0 this #is #a #test
1 another #test None None
Run Code Online (Sandbox Code Playgroud)
编辑1:实际用例是保持匹配模式,例如:
serie.str.split(r'\n\*\*\* [A-Z]+', expand=True)
Run Code Online (Sandbox Code Playgroud)
在我的情况下,[AZ] +是处理步骤,我想保留这些步骤以进行进一步处理。
您可以通过正面看待来分裂。因此,分割点将是前瞻性表达之前的点。
import pandas as pd
serie = pd.Series(['this#is#a#test', 'another#test'])
print(serie.str.split('(?=#)', expand=True))
Run Code Online (Sandbox Code Playgroud)
输出值
0 1 2 3
0 this #is #a #test
1 another #test None None
Run Code Online (Sandbox Code Playgroud)