小编old*_*ard的帖子

在数据帧上迭代re.split()

我试图使用re.split()将pandas数据框中的单个变量拆分为另外两个变量.

我的数据如下:

   xg              
0.05+0.43
0.93+0.05
0.00
0.11+0.11
0.00
3.94-2.06
Run Code Online (Sandbox Code Playgroud)

我想创造

 e      a
0.05  0.43
0.93  0.05
0.00  
0.11  0.11
0.00
3.94  2.06
Run Code Online (Sandbox Code Playgroud)

我可以使用for循环和索引来完成此操作.

for i in range(len(df)):
    if df['xg'].str.len()[i] < 5:
        df['e'][i] = df['xg'][i]
    else:
        df['e'][i], df['a'][i] = re.split("[\+ \-]", df['xg'][i])
Run Code Online (Sandbox Code Playgroud)

然而,这很慢,我不相信这是一个很好的方法,我正在努力提高我的代码/ python理解.

我尝试使用np.where编写它,或使用列表理解或应用lambda进行了各种尝试,但我不能让它运行得太多.我认为我遇到的所有问题都是因为我试图将函数应用于整个系列而不是位置值.

如果有人知道一个比我丑陋的循环更好的方法,我会非常感兴趣.

python regex loops python-3.x pandas

6
推荐指数
1
解决办法
744
查看次数

标签 统计

loops ×1

pandas ×1

python ×1

python-3.x ×1

regex ×1