I would like to replace all numbers in description and summary columns with text. For example: A 58-year-old African-American woman presents... should be A fifty eight years old.... I want all the numbers to be converted like this. Please see attached the screenshot.
Asked
Active
Viewed 240 times
-1
-
2Welcome to stack overflow. Please [edit] your question to include your sample input and expected output as text in your question, not as a picture or link, to make a [mcve], along with _code_ for what you've tried so far based on your own research. See [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Mar 10 '21 at 23:31
2 Answers
1
I suggest using the num2words library: https://pypi.org/project/num2words/.
You would first find the location of the number in the string. After that, you can use num2words to convert it:
from num2words import num2words
num2words(number)

QWERTYL
- 1,355
- 1
- 7
- 11
-
Thanks for your input. when I loop over the each sentence in the description columns( as shown in the picture) each number is str but num2words() take integer how to I find the numbers a integer? Thanks – Emrul Mar 11 '21 at 01:04
-
I believe you can pass a string of a number to num2words. To extract the number from the string, you would first need to convert the hyphens to spaces using `string = string.replace("-", " ")` and then you could use the following to extract a number: `number = [int(i) for i in your_string.split() if i.isdigit()][0]`. (source: https://www.geeksforgeeks.org/python-extract-numbers-from-string/) – QWERTYL Mar 11 '21 at 01:46
0
from num2words import num2words
word = num2words(num)
https://pypi.org/project/num2words/
It depend how your code is set up, but using this will be the easiest way

Brandon Kauffman
- 1,515
- 1
- 7
- 33