替换熊猫数据框中的值

时间:2019-08-14 08:33:56

标签: python pandas dataframe replace

我以为我了解替换功能,但似乎我没有。请在下面查看我的代码。我只想用NaN替换所有-999值(或者使NULL更有意义?),但是输出仍然在所有Dataframe中包含-999。我想念什么?

          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()

3 个答案:

答案 0 :(得分:1)

我认为您应该使用以下代码:

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

答案 1 :(得分:1)

似乎您没有将具有替换值的对象列分配给数据框。使用:

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

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

此答案假定dfWeather包含开头的数字值。 如果继续处理数据,则使用np.nan而不是0将提供更好的处理。

答案 2 :(得分:1)

Encountered the symbol FROM when expecting one of the following pl sql

cursor o1 is 

select substr(tarifa,1,2), count(*)
from pol p, uvod u, doppov d
where extract(year FROM datum_dop) = EXTRACT(YEAR FROM sysdate) 
and izdavanje >='1-jul-13'
and p.orgjed = u.sorgz (+)
and DATUM_PREKIDA is not null
and p.polica=d.polica and d.pov_dopl='P'
and d.status='F'
and cisti_ao(p.polica)!=0 
group by substr(tarifa,1,2);