0

given a column of strings in a dataframe, the following code transforms them into integers. What I want to do is to just leave the string part without the dot, and whenever the cell contains a number in string form, I would like to change it to a string called 'number'. Just to be clear, the cells in this column have the following values:

'a. 12','b. 75','23', 'c/a 34', '85', 'a 32', 'b 345'

and I want to replace the cell values in this column with the following:

'a', 'b', 'number', 'c/a', 'number', 'a' , 'b' 

How do I do that?

l2=['a. 12','b. 75','23', 'c/a 34', '85', 'a 32', 'b 345']
d = {'col1': []}
df = pd.DataFrame(data=d)
df['col1']=l2

df['col1'] = df['col1'].str.replace(r'\D', '').astype(str)
print(df)
1
  • What I have tried is above. I unfortunately did not know how to change my code. Commented Oct 11, 2021 at 19:10

1 Answer 1

1

According to your example which seems to be (1) change numbers only to 'number' and (2) remove trailing dot/space/numbers:

df['col1'] = df['col1'].str.replace(r'^[\d\s]+$', 'number', regex=True).str.replace('\.?\s*\d*$', '')

output:

     col1
0       a
1       b
2  number
3     c/a
4  number
5       a
6       b
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, and how do you preserve the original number instead of replacing it with 'number' ?
The I would remove the first replace and either make the space mandatory \s+ in the second replace or add a mandatory something at the beginning, depending on your use case. Give me more details if you need help building this

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.