1

I need to convert all my columns that are of the data type objects into strings.

For simplicity, here is the column headers:

keys= [Name, Age, Passport, Date, Location, Height]

'Name' and 'Location' are of type object. I need to identify these columns are of type object, and convert the columns into strings.

My loop that I attempted to write:

while variable_1 < len(keys):
  if df[variable_1].dtypes == object:
    df[variable_1] = df[variable_1].astype(str)
    variable_1 = variable_1 + 1

This is throwing an obvious error, I am sort of stuck on the syntax.

0

3 Answers 3

3

I am not sure why you would want to do this, but here's how:

object_columns = (df.dtypes == numpy.object)
df.loc[:, object_columns] = df.loc[:, object_columns].astype(str)

If you ever use a loop in Pandas, you are 99% surely doing it wrong.

Sign up to request clarification or add additional context in comments.

Comments

2

Consider the dataframe df

df = pd.DataFrame(dict(
        A=[1, 2, 3],
        B=[[1], [2], [3]],
        C=['1', '2', '3'],
        D=[1., 2., 3.]))

Use applymap with type to see the types of each element.

df.applymap(type)

               A               B              C                D
0  <class 'int'>  <class 'list'>  <class 'str'>  <class 'float'>
1  <class 'int'>  <class 'list'>  <class 'str'>  <class 'float'>
2  <class 'int'>  <class 'list'>  <class 'str'>  <class 'float'>

Using select_dytpes to take just object columns and update to update the dataframe

df.update(df.select_dtypes(include=[np.object]).astype(str))

df.applymap(type)

               A              B              C                D
0  <class 'int'>  <class 'str'>  <class 'str'>  <class 'float'>
1  <class 'int'>  <class 'str'>  <class 'str'>  <class 'float'>
2  <class 'int'>  <class 'str'>  <class 'str'>  <class 'float'>

3 Comments

This seems to be working, but when I print this command 'print(df.Name.dtypes)' it still comes out as an object. But I did get exactly what I wanted when I printed the 'df.applymap(type)'.
@rmahesh pandas does not have a dtype that is str. It wraps up string, lists and anything else that isn't a numeric type as object. The individual types underneath can be anything.
Thank you! That explains why it was coming out as an object.
1

why you are usingwhile ? rather than simply using for ?

for i in df.columns:
    if df[i].dtype==object:
        df[i]= df[i].astype(str)

3 Comments

When I ran print(df.Name.dtypes), it still came out as an object.
and if you do want it get you something like str, you can run this type(df.Name[1])
Thank you, that seems to be where I was going wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.