出典DF:
In [37]: df
Out[37]:
Components Outcome
0 (Quantity__(0.0, 16199.0]) (UnitPrice__(-1055.648, 3947.558])
1 (UnitPrice__(-1055.648, 3947.558]) (Quantity__(0.0, 16199.0])
ソリューション:
In [38]: cols = ['Components','Outcome']
...: df[cols] = df[cols].replace(r'\(([^_]*)__\(([^,\s]+),\s*([^\]]+)\]\).*',
...: r'\2 < \1 <= \3',
...: regex=True)
結果:
In [39]: df
Out[39]:
Components Outcome
0 0.0 < Quantity <= 16199.0 -1055.648 < UnitPrice <= 3947.558
1 -1055.648 < UnitPrice <= 3947.558 0.0 < Quantity <= 16199.0
UPDATE:
In [113]: df
Out[113]:
Components Outcome
0 (Quantity__(0.0, 16199.0]) (UnitPrice__(-1055.648, 3947.558])
1 (UnitPrice__(-1055.648, 3947.558]) (Quantity__(0.0, 16199.0])
In [114]: cols = ['Components','Outcome']
In [115]: pat = r'\s*\(([^_]*)__\(([^,\s]+),\s*([^\]]+)\]\)\s*'
In [116]: df[cols] = df[cols].replace(pat, r'\2 < \1 <= \3', regex=True)
In [117]: df
Out[117]:
Components Outcome
0 0.0 < Quantity <= 16199.0 -1055.648 < UnitPrice <= 3947.558
1 -1055.648 < UnitPrice <= 3947.558 0.0 < Quantity <= 16199.0
または括弧witout:
In [119]: df
Out[119]:
Components Outcome
0 Quantity__(0.0, 16199.0]) UnitPrice__(-1055.648, 3947.558]
1 UnitPrice__(-1055.648, 3947.558] Quantity__(0.0, 16199.0]
In [120]: pat = r'([^_]*)__\(([^,\s]+),\s*([^\]]+)\]'
In [121]: df[cols] = df[cols].replace(pat, r'\2 < \1 <= \3', regex=True)
In [122]: df
Out[122]:
Components Outcome
0 0.0 < Quantity <= 16199.0) -1055.648 < UnitPrice <= 3947.558
1 -1055.648 < UnitPrice <= 3947.558 0.0 < Quantity <= 16199.0
は、文字列型のすべての列はありますか?あなたが 'type(df.Components.iloc [0])'のときに何を得ますか? – Psidom
null以外のオブジェクト – Student