简体   繁体   English

替换 Pandas 数据框中的值

[英]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.我只想用 NaN 替换所有 -999 值(或者让 NULL 更有意义?)但输出仍然包含所有数据帧中的 -999。 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()

我认为你应该使用这个代码:

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

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.此答案假定 dfWeather 包含开始的数值。 Using np.nan instead of 0 offers better handling if you continue processing the data.如果您继续处理数据,使用 np.nan 而不是 0 可以提供更好的处理。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM