UPDATE
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df.iloc[:,3].replace(r'(?<!\S)\d+(?!\S)', lambda x: p.number_to_words(x.group()), regex=True, inplace=True)
df.iloc[:,3].head(2)
0 15
1 89
Name: D, dtype: int64
df = df.astype(str)
df.iloc[:,3].replace(r'(?<!\S)\d+(?!\S)', lambda x: p.number_to_words(x.group()), regex=True, inplace=True)
df.iloc[:,3].head(2)
0 <function <lambda> at 0x7fd8a6b4db18>
1 <function <lambda> at 0x7fd8a6b4db18>
Name: D, dtype: object
I got a pandas data frame and some of the rows contains numbers in some columns. I want to use the inflect library to replace only the numbers with corresponding word representation.
I think df.replace is a good fit. But how can I specify that only numbers (all the numbers which are separated by white spaces) should be replaced and pass it as argument to inflect ?.
p = inflect.engine()
df.replace(r' (\d+) ', p.number_to_words($1), regex=True, inplace=True)
Similarly, I have second dataframe, where I want to do it for a specific column, column with index 4. The column contains just 4 digit numbers only (year). How can I do it ?.