Middle
There is a column with numbers in a DataFrame, I want to convert it to string type. How to do it? And if not using astype?
sobes.tech AI
Answer from AI
To convert a column with numbers in a DataFrame to a string type, you typically use the astype(str) method, for example:
import pandas as pd
df['column'] = df['column'].astype(str)
If astype cannot be used, you can apply the apply method with the str function:
df['column'] = df['column'].apply(str)
Or use the map method:
df['column'] = df['column'].map(str)
All these methods convert numeric values to strings, with apply and map offering more flexibility for custom conversions.