Replace dots in a float column with nan in Python

前端 未结 1 1094
情话喂你
情话喂你 2021-01-14 16:50

I have a data frame df like this

df = pd.DataFrame([
    {\'Name\': \'Chris\', \'Item Purchased\': \'Sponge\', \'Cost\': 22.50},
    {\'Name\': \'Kevyn\', \'         


        
相关标签:
1条回答
  • 2021-01-14 17:04

    Use DataFrame.replace with the regex=True switch.

    df = df.replace('\.+', np.nan, regex=True)
    df
    
             Cost Item Purchased   Name
    Store 1  22.5         Sponge  Chris
    Store 1   NaN   Kitty Litter  Kevyn
    Store 2   NaN          Spoon  Filip
    

    The pattern \.+ specifies one or more dots. You could also use [.]+ as a pattern to the same effect.

    0 讨论(0)
提交回复
热议问题