#๐ Graph issues matplotlib and pandas
27 messages ยท Page 1 of 1 (latest)
@tawny pawn
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Hey @tawny pawn!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
def lineChartM(df2): ## Makes the Line graph
for i in df2.index:
if (df2.loc[i,'gender']) == ("female"): #Removes all the female
df2 = df2.drop(i)
df2 = df2.round(decimals=0)
plt.figure(figsize=(10, 6))
df2 =df2.sort_values(by= 'daily_social_media_hours',ascending=True)
df2 =df2
plt.plot(df2['daily_social_media_hours'],
df2['stress_level'],
marker='o',
linestyle='-',
label='Stress Level')
plt.title("Stress level over daily social media hours in male teens")
plt.xlabel("Daily social media hours")
plt.ylabel("Stress levels")
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
return convertGraphToImage()
df6 = pd.read_csv('TeenMentalHealthClean.csv')
def lineChartF(df6): ## Makes the Line graph
for i in df6.index:
if (df6.loc[i,'gender']) == ("male"):
df6 = df6.drop(i) #removes all the male values
plt.figure(figsize=(10, 6))
df6 = df6.round(decimals=0)
df6 =df6.sort_values(by= 'daily_social_media_hours',ascending=True)
plt.plot(df6['daily_social_media_hours'],
df6['stress_level'],
marker='o',
linestyle='-',
label='Stress Level')
plt.title("Stress level over daily social media hours in female teens")
plt.xlabel("Daily social media hours")
plt.ylabel("Stress levels")
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
return convertGraphToImage()
You need a groupby
yeah i thought so but when i was trying a groupby.mean() it was giving me a str error
"TypeError: dtype 'str' does not support operation 'mean'"
You need filter the column on which you want to compute the mean
Something like
df.groupby("key_column")["target_column"].mean()
No sure about the syntax exactly but that's the idea
do not assign it to df6, you'll overwrite the df
Unless you don't need it anymore
But I would say yes, try it
group according to the X axis. For each X axis point (social media hours), you want to group the sample values (stress levels reported for that number of social media hours) to get their mean.
Incidentally,
for i in df6.index:
if (df6.loc[i,'gender']) == ("male"):
df6 = df6.drop(i) #removes all the male values
putting Pandas data into your own loop is a red flag. It will generally be much slower than letting Pandas handle the looping for you.
For this kind of filtering, see https://stackoverflow.com/questions/17071871
Oh ty.
KeyError: 'daily_social_media_hours'
Currently upon grouping it i get this error depending on which is first in the group by
well, that will require standard debugging techniques, and looking at the data you actually have.
Ty I think i got it working
adding .reset_index() seems to have fixed it
Ty both so muchh
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.