0

My pandas df:

      first   second    third fourth fifth
0       x       y        12     2      0
1       a       b         1     1      1
2       c       d         2    12      0

To check column data types, I use:

df.info()

It is result:

<class 'pandas.core.frame.DataFrame'>
first    380 non-null object
second   380 non-null object
third    380 non-null object
fourth   380 non-null object
fifth    380 non-null int32

I want to convert first and second column into string:

df['first'] = df['first'].astype(str)
df['second'] = df['second'].astype(str)

I want to check that:

df.info()

first    380 non-null object
second   380 non-null object
third    380 non-null object
fourth   380 non-null object
fifth    380 non-null int32

So it does not worked, where did I made a mistake?

1

1 Answer 1

-1

Pandas API has slightly changed. Just try:

df = df.astype("string")

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

6 Comments

Can you add the link to the documentation where you read about that please?
@CarloZanocco, here is a reference: pandas.pydata.org/pandas-docs/stable/user_guide/…
Thank you! Just another question, why printing df.info() show first 380 non-null object and not first 380 non-null string?
I do not want to make all columns a string, just first and second. Where need I to put columns name, which I want to convert?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.