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,332
  • 2
  • 13
  • 31
  • 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

3 Answers3

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
  • 655
  • 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
1

Say you have a column source_name with values like : 'Anand_VUNagar_DC (Gujarat)' and you want to create 3 new columns A,B,B with values : 'Mother', 'Teresa', 'India' You can do by-

df1[['A','B','C']]=df1['source_name'].str.rsplit('_',2, expand=True)

result:

    source_name A   B   C
    0   Anand_VUNagar_DC (Gujarat)  Anand   VUNagar DC (Gujarat)
Riya
  • 11
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 15 '22 at 14:23
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
  • 317,841
  • 20
  • 164
  • 234
  • 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