royalewithcheese
royalewithcheese

Reputation: 502

How to remove trailing zeros using pandas only if the column's dtype is numeric?

Suppose I have a dataframe like this,

A        |  B
2.000000 | "hello 1.0"
3.00000  | "mellow"
         | "trello 9.0 elo"
4.0      | "cello 3.00"
         

How can I get the output like this,

A   |  B
2   | "hello 1.0"
3   | "mellow"
    | "trello 9.0 elo"
4   | "cello 3.00"

I want to convert all columns dtypes to string; however, I want to be able to remove the trailing zeros only if the column's dtype is numeric.

There was one solution where you could use lambda function but I do not exactly remember the format.

So far I have this,

df[base_column].astype(str).str.replace(‘.0’, ‘ ‘).replace('nan', np.nan).replace('None', np.nan)

but this code converts column B also from hello 1.0 to hello 1

Any help would be appreciated!

Upvotes: 0

Views: 367

Answers (2)

wheezay
wheezay

Reputation: 101

this will only make only numerics as int sparing strings

 def func(row):
        if type(row[0])!=str:
            return (int(row[0]))
 df.apply(func,axis=1)

Upvotes: 0

jezrael
jezrael

Reputation: 863301

I guess reason for .0 values are missing values, so here is possible use integer_na:

cols = df.select_dtypes(np.number).columns
df[cols] = df[cols].astype('Int64')

If need replace all numeric to strings with removed trailing .0 use:

cols = df.select_dtypes(np.number).columns
df[cols] = df[cols].astype(str).replace('\.0','',regex=True).replace(['nan','None'], np.nan)

Upvotes: 1

Related Questions