1

I am looking for suggestions as to how to get python to convert an empty space within a sub-string to a dash (-).

In the below dataframe column A has the raw data and need to add a dash to the sub-strings that have a space in them to get column B

Any suggestions how to overcome this?

enter image description here

1
  • 2
    df.A.str.replace(' ','-') is what you want Commented Apr 25, 2019 at 16:10

1 Answer 1

2

How about replace:

>>> import pandas as pd
>>> df = pd.DataFrame(data={'A': ['ADMCAP B', 'ATHENA', 'ATLA DKK', 'BNORDIK CSE']})
>>> df
             A
0     ADMCAP B
1       ATHENA
2     ATLA DKK
3  BNORDIK CSE
>>> df['B'] = df['A'].replace(' ', '-', regex=True)
>>> df
             A            B
0     ADMCAP B     ADMCAP-B
1       ATHENA       ATHENA
2     ATLA DKK     ATLA-DKK
3  BNORDIK CSE  BNORDIK-CSE

Or you could use: df['B'] = df['A'].str.replace(' ', '-')

Sign up to request clarification or add additional context in comments.

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.