How do you convert int to dataframe in python?

Depending on your needs, you may use either of the 3 approaches below to convert integers to strings in Pandas DataFrame:

(1) Convert a single DataFrame column using apply(str):

df['DataFrame Column'] = df['DataFrame Column'].apply(str)

(2) Convert a single DataFrame column using astype(str):

df['DataFrame Column'] = df['DataFrame Column'].astype(str)

(3) Convert an entire DataFrame using applymap(str):

df = df.applymap(str)

Let’s now see the steps to apply each of the above approaches in practice.

Step 1: Collect the Data to be Converted

To start, collect the data that you’d like to convert from integers to strings.

For illustration purposes, let’s use the following data about products and their prices:

Product Price
ABC 350
DDD 370
XYZ 410

The goal is to convert the integers under the ‘Price’ column into strings.

Step 2: Create the DataFrame

Next, create the DataFrame to capture the above data in Python.

Here is the code to create the DataFrame for our example:

import pandas as pd

data = {'Product': ['ABC','DDD','XYZ'],
          'Price': [350,370,410]
        }

df = pd.DataFrame(data)
print (df)
print (df.dtypes)

Once you run the code in Python, you’ll see that the ‘Price’ column is set to integers:

  Product  Price
0     ABC    350
1     DDD    370
2     XYZ    410
Product    object
Price       int64
dtype: object

Step 3: Convert the Integers to Strings in Pandas DataFrame

Finally, you can use the apply(str) template to assist you in the conversion of integers to strings:

df['DataFrame Column'] = df['DataFrame Column'].apply(str)

For our example, the ‘DataFrame column’ that contains the integers is the ‘Price’ column.

Therefore, the full Python code to convert the integers to strings for the ‘Price’ column is:

import pandas as pd

data = {'Product': ['ABC','DDD','XYZ'],
          'Price': [350,370,410]
        }

df = pd.DataFrame(data)
df['Price'] = df['Price'].apply(str)

print (df)
print (df.dtypes)

Run the code, and you’ll see that the ‘Price’ column is now set to strings (i.e., where the data type is now object):

  Product Price
0     ABC   350
1     DDD   370
2     XYZ   410
Product    object
Price      object
dtype: object

Alternatively, you may use the astype(str) approach to perform the conversion to strings:

df['DataFrame Column'] = df['DataFrame Column'].astype(str)

So the full Python code would look like this:

import pandas as pd

data = {'Product': ['ABC','DDD','XYZ'],
          'Price': [350,370,410]
        }

df = pd.DataFrame(data)
df['Price'] = df['Price'].astype(str)

print (df)
print (df.dtypes)

As before, you’ll see that the ‘Price’ column now reflects strings:

  Product Price
0     ABC   350
1     DDD   370
2     XYZ   410
Product    object
Price      object
dtype: object

Convert the Entire DataFrame to Strings

Let’s say that you have more than a single column that you’d like to convert from integers to strings.

For example, let’s suppose that you have the following dataset with 3 columns:

Product Price Original Cost
ABC 350 200
DDD 370 230
XYZ 410 280

The goal is to convert the last two columns (i.e., the ‘Price’ and ‘Original Cost’ columns) from integers to strings.

Here is how DataFrame would look like:

import pandas as pd

data = {'Product': ['ABC','DDD','XYZ'],
        'Price': [350,370,410],
        'Original Cost': [200,230,280]
        }

df = pd.DataFrame(data)

print (df)
print (df.dtypes)

Run the code, and you’ll see that the last two columns are currently set to integers:

  Product  Price  Original Cost
0     ABC    350            200
1     DDD    370            230
2     XYZ    410            280
Product          object
Price             int64
Original Cost     int64
dtype: object

In that case, you may use applymap(str) to convert the entire DataFrame to strings:

df = df.applymap(str)

Here is the complete code for our example:

import pandas as pd

data = {'Product': ['ABC','DDD','XYZ'],
        'Price': [350,370,410],
        'Original Cost': [200,150,100]
        }

df = pd.DataFrame(data)
df = df.applymap(str)

print (df)
print (df.dtypes)

Run the code, and you’ll see that all the columns in the DataFrame are now strings:

  Product Price Original Cost
0     ABC   350           200
1     DDD   370           150
2     XYZ   410           100
Product          object
Price            object
Original Cost    object
dtype: object

You may also wish to check the following tutorials that review the steps to convert:

  • Strings to integers
  • Strings to floats
  • Integers to floats

How do I convert numbers to pandas?

The best way to convert one or more columns of a DataFrame to numeric values is to use pandas. to_numeric() . This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

How do you convert an int data type into a string data type in pandas?

There are four ways of converting integers to strings in pandas..
Method 1: map(str) frame['DataFrame Column']= frame['DataFrame Column'].map(str).
Method 2: apply(str) frame['DataFrame Column']= frame['DataFrame Column'].apply(str).
Method 3: astype(str) ... .
Method 4: values.astype(str) ... .
Output:.

How do you convert int to string in Python?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string.

Can you convert a list to DataFrame in Python?

As we all know that data in python is mostly provided in the form of a List and it is important to convert this list into a data frame. If you want to practice pandas skills then you can check out Pandas exercises for Beginners.