A concise function to transfer text to number in DataFrame

Annie Wang
2 min readJun 26, 2021
Photo by the author

Today I want to share a concise function that can transfer text to numbers. Let’s show the original and target data first to get a direct impression.

The original dataset is like below:

The target dataset is as below:

The dataset analysis

It shows which customer has purchased which products, of course, the dataset can be expanded by adding more information(columns), like time, and buy amount, etc, to show more detailed information. I want to focus on the main problem, so I delete other information.

The customer id has been anonymized, not so easy to analyze, so I would like to transfer them into numbers. From the datasets, we can see that nor the product id neither customer id is unique.

The function :

--

--