1

I'm using a pandas data frame to compare some weather data and some pollution levels.

I need a hand because I'm plotting some data, but one of my data points is out of range and messing with my plot.

I am using a bar plot, and I would just set the y limits of the plot, however the bar for this data point still shows up and just goes out of range.

What to do to get rid of this single value? I have tried setting it to 0 but it comes up with a warning about A value is trying to be set on a copy of a slice from a DataFrame.

here is what the plot looks like, and here is some of the code that goes with it:

fig, ax = plt.subplots()
#defining the plot type and the data to be plotted. 
ax.bar(df1["Pressure (hPa)"].loc["2019"],
           df2["PM10 particulate matter (Hourly measured)"].loc["2019"],
           alpha=0.2,
          )

ax.bar(df1["Pressure (hPa)"].loc["2019"],
        df2["PM2.5 particulate matter (Hourly measured)"].loc["2019"],
      alpha=0.2,
      color="Green")

#setting the limits of the x axis to make the graph more readable. 
ax.set_xlim(970,1045)

The out of range value is in the df2. I'm just not sure how to remove it. I have tried doing

df2[df2["PM2.5 particulate matter (Hourly measured)"]>150] = 0 

but that comes up with the warning message.

I have tried using < operators too directly on the data frame, but it just gives me boolean values which I can't plot :(

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Elirbs
  • 11
  • 1
  • 1
    Does `df2.loc[df2["PM2.5 particulate matter (Hourly measured)"]>150] = 0` solve your problem ? – manu190466 Apr 21 '23 at 11:40
  • I assume you are getting a `SettingWithCopyWarning` with the solution manu190466 provided. If so, you can safely ignore that warning in this case. It is only a warning, not an error. See [this post](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) for more details on that warning. – psalt Apr 21 '23 at 11:46
  • @manu190466 yes it does, but comes out with a warning, I'm just wondering if there is a better alternative... – Elirbs Apr 22 '23 at 10:24

1 Answers1

0

you can try, example:

import pandas as pd
import seaborn as sns

data = {
  "calories": [420, 380, 390, 1200],
  "day": [1, 2, 3, 4]
}
df = pd.DataFrame(data)
#filter:
df = df.loc[df['calories'] < 500]
sns.barplot(data=df, x="day", y="calories")
  • No because there is a shape missmatch between the two... I think I *have* to have the "space" there as it were, just with no value... – Elirbs Apr 22 '23 at 10:27