Vlad
Vlad

Reputation: 560

Pandas.DataFrame.str.replace function replaces floats to NaN

I have a Pandas DataFrame, suppose: df = pd.DataFrame({'Column name':['0,5',600,700]})

I need to remove ,. The code is: df_mod = df.stack().str.replace(',','').unstack()

As a result I get: [05, NaN, NaN]

Do you have any ideas why my expression replaces numbers with NaN and how to avoid it? Thanks a lot!

Upvotes: 13

Views: 13885

Answers (3)

piRSquared
piRSquared

Reputation: 294576

As identified by @Psidom, you get NaN because ints don't have a replace method. You can run it as is and fill in those Nan values with the original column

c = 'Column name'
df[c].str.replace(',', '').fillna(df[c])

0     05
1    600
2    700
Name: Column name, dtype: object

This preserves all the dtypes

Upvotes: 6

Yeile
Yeile

Reputation: 761

I have an alternate answer, just for fun:

df.applymap(lambda x: x.replace(',','') if type(x) is str else x)

This would check if every value for str type, then .replace for any str.

Upvotes: 5

akuiper
akuiper

Reputation: 215137

Those numbers are treated as numeric values, which don't have str.replace methods, you can convert the column to string, remove the comma, and then convert the data type back:

df['Column name'].astype(str).str.replace(",", "").astype(int)

#0      5
#1    600
#2    700
#Name: Column name, dtype: int64

Upvotes: 20

Related Questions