1

I have pandas dataframe include a column a with content like this '<152/abcx>' ,'<42/da>', '<2/kiw>'. What I want to do is based on the content , remove "<",">", and create two new separate column like this column b : 152 ; 42; 2, column c: 'abcx', 'da', 'kiw'.

df.a.str[df.a.str.find('<')+1:df.a.str.find('/')-1]

the code I tried doesn't work

2 Answers 2

2

Try using this code:

df = pd.DataFrame({'a': ['<152/abcx>' ,'<42/da>', '<2/kiw>']})
df = df['a'].str.strip('<>').str.split('/', expand=True)
df.columns = ['columnb', 'columnc']
print(df)

Output:

  columnb columnc
0     152    abcx
1      42      da
2       2     kiw
Sign up to request clarification or add additional context in comments.

Comments

0

This is the solution I guess you are finding:

import pandas as pd 
def f(x):
    x = x.split('/')
    return pd.Series([x[0].replace('<',''),x[1].replace('>','')])


df =pd.DataFrame({'a':['<152/abcx>' ,'<42/da>', '<2/kiw>']})
df[['c1','c2']] = df['a'].apply(f)
print(df)

Output:

            a   c1    c2
0  <152/abcx>  152  abcx
1     <42/da>   42    da
2     <2/kiw>    2   kiw
[Finished in 2.7s]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.