0

my code is this:

 plt.pie(df['Rainfall'].value_counts().values,

        labels = df['Rainfall'].value_counts().index,

        autopct='%1.1f%%')

   plt.show()

  df.groupby('Rainfall').mean()

The issue I am having is that I am trying to show the probability of it raining however its giving many different values. All the values that are above 0 should be a yes that it will rain and all below should say it wont.

I am not sure how to do this and to separate the two

im following this guide: https://www.geeksforgeeks.org/rainfall-prediction-using-machine-learning-python/

The pie chart is attached below

enter image description here

I hope you can help!

Tried to follow the guide and read the documentation but am completely lost

1 Answer 1

0

I advice you to use the .apply() method and a lambda function in order to easily classify between positive and negative values.

For example:

import pandas as pd
import matplotlib.pyplot as plt

data = {"location": [1, 1, 4, 3, 2], "Rainfall": [0, 0, 0.1, 0.04, 0.0001]}
df = pd.DataFrame(data)
df["Rainfall"] = df["Rainfall"].apply(lambda x: "YES" if x > 0 else "NO")


plt.pie(df["Rainfall"].value_counts().values, labels=df["Rainfall"].value_counts().index, autopct="%1.1f%%")
plt.show()

will output this

Pie chart

2
  • Hi, Thanks for this. it now shows yes and no. However how do I connect this with my CSV file which is using the column Rainfall instead of using the values as a string?
    – John.R
    Commented Jul 15, 2024 at 23:41
  • Ah no worries I found it I will just use pandas to import the CSV and use that as the data frame. Thanks for the help!
    – John.R
    Commented Jul 15, 2024 at 23:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.