covnert all dataframe values from int to str in python

To convert all values in a Pandas dataframe from integer to string datatype in Python, you can use the astype() method with 'str' as the argument. Here's an example:

main.py
import pandas as pd

# Create a sample dataframe
data = {'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]}
df = pd.DataFrame(data)

# Convert all columns to string datatype
df = df.astype(str)

# Verify the datatype of all columns
print(df.dtypes)
255 chars
12 lines

Output:

main.py
col1    object
col2    object
col3    object
dtype: object
59 chars
5 lines

In the above code, first we import the Pandas library. Then we define a sample dataframe df with integer values in each column. Finally, we use the astype() method to convert all the columns to the string datatype, and then print the datatype of all columns in the dataframe.

gistlibby LogSnag