0

I have the following data frame:

df = pd.DataFrame()
df['Name'] = ['A','B','C']
df['Value'] = ['2+0.5','1+0.2','2-0.06']

What I wanted to do is to split the value and assign to two new columns. It means my desired output will be as follow: The element in value column will be splitted into two and I will re-use the sign in the columns.

I am very grateful for your advice.

Thanks.

enter image description here

Zephyr
  • 1,312
  • 1
  • 12
  • 24
  • 1
    Possible duplicate of [How to split a column into two columns?](https://stackoverflow.com/questions/14745022/how-to-split-a-column-into-two-columns) – Gimhani Oct 10 '18 at 03:10

2 Answers2

3
import pandas as pd
df = pd.DataFrame()
df['Name'] = ['A','B','C']
df['Value'] = ['2+0.5','1+0.2','2-0.06']
df[['value1','value2']]=df.Value.str.split('[-+]+',expand=True)
contain_symbol = df.Value.str.contains('-',regex=False)
df.loc[contain_symbol,"value2"] = -df.loc[contain_symbol,"value2"].astype(float)
tianhua liao
  • 617
  • 5
  • 9
  • Thanks for your prompt response on the question . Really appreciate it – Zephyr Oct 10 '18 at 03:52
  • Thanks Tianhua for the advice. let's say I want to split a string at unique sprint. How can i do it. for example, my string contains abcdf 0:2 dfrgh, I want to split at 0:2. (i.e. I have two string **abcdf** and **dfrgh**. However, the splitter **0:2** is different for every row. It could be 3:0, 1:2,3:3 and so many combination.How can I do it? – Zephyr Oct 12 '18 at 05:52
  • `df.Value.str.split('[-+]+',expand=True)` This split could accept regular expression pattern to find and split with found result. So you could extract the general pattern for these **splitter**. If there are **0:2 3:0 1:2 3:3**, you could use pattern like `[0-9]:[0-9]`, it will match all kinds of splitter like `number:number`. – tianhua liao Oct 12 '18 at 09:02
  • Thanks. I will try – Zephyr Oct 12 '18 at 09:05
0

IIUC

newdf=df.Value.str.split('([-+])+',expand=True)
newdf[2]=newdf[1].map({'+':1,'-':-1})*newdf[2].astype(float)
df[['value1','value2']]=newdf[[0,2]]
df
Out[30]: 
  Name   Value value1  value2
0    A   2+0.5      2    0.50
1    B   1+0.2      1    0.20
2    C  2-0.06      2   -0.06
BENY
  • 296,997
  • 19
  • 147
  • 204
  • he wants to maintain the negative on the 0.06. – d_kennetz Oct 10 '18 at 03:49
  • Thanks for the advice. How can I include minus sign in 0.06 – Zephyr Oct 10 '18 at 03:50
  • Hi Wen, Thanks a lot for your advice, I appreciate much. I have slightly different splitting as follow: let's say I want to split a string at unique sprint. How can i do it. for example, my string contains abcdf 0:2 dfrgh, I want to split at 0:2. (i.e. I have two string abcdf and dfrgh. However, the splitter 0:2 is different for every row. It could be 3:0, 1:2,3:3 and so many combination.How can I do it? – Zephyr Oct 12 '18 at 05:56