G.M
G.M

Reputation: 365

Replacing Values in an Pandas Dataframe

I assumed I understood the replace Function but seemingly I didnt. Please see my code below. I just want to replace all -999 values with NaN (or makes NULL more sense?) but the out put still contains -999 in all Dataframes. What am I missing?

          def SQLtoPandas(Connection,SQLString):
                df =pd.read_sql(SQLString, con=Connection)
                return df

            WeatherString = "select * FROM weather" 
    dfWeather = SQLtoPandas(Connection, WeatherString)

            RainkindsString = "select * FROM Rainkinds" 
    dfRainkinds = SQLtoPandas(Connection, RainkindsString)

            StationsString = "select * FROM Stations" 
    dfStations = SQLtoPandas(Connection, StationsString)

            #here is the important part. As stated, maybe replacing wiht NULL makesm ore sense? 
dfWeather.replace(-999, 0)

            #---------------------------Output Data---------------------------------------- 
        def DatenAnalyse():    
                pd.set_option('display.max_columns', None)  

                print("\n --> Zusammenfassung Wetterdaten <-- \n" )
                print(dfWeather.describe())
                print("\n --> Beispiel Wetterdaten <-- \n" )
                print(dfWeather.head(10))

                print("\n ----------------------------------------------------------------")
                print("\n \n --> Zusammenfassung Regenarten <-- \n" )
                print(dfRainkinds.describe())
                print("\n --> Beispiel Regenarten <-- \n" )
                print(dfRainkinds.head(10))

                print("\n ----------------------------------------------------------------")
                print("\n \n --> Zusammenfassung Stationen <-- \n" )
                print(dfStations.describe())
                print("\n --> Beispiel Stationen <-- \n" )
                print(dfStations.head(10))

            DatenAnalyse()

Upvotes: 1

Views: 1117

Answers (3)

moys
moys

Reputation: 8033

import numpy as np
df['Weather'] = df['Weather'].replace(-999, np.nan, inplace=True)

Upvotes: 1

cptnJ
cptnJ

Reputation: 230

it seems that you do not assign the object-column with the replaced values to your dataframe. Use:

#here is the important part. As stated, maybe replacing wiht NULL makesm ore sense? 

dfWeather.replace(-999, 0, inplace=True)

This answer assumes that dfWeather contains numeric values to begin with. Using np.nan instead of 0 offers better handling if you continue processing the data.

Upvotes: 1

ivallesp
ivallesp

Reputation: 2202

I think you should use this code:

dfWeather = dfWeather.replace(-999, np.nan)

Upvotes: 1

Related Questions