Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If I want to split data in one part of a csv file in pandas by another column in the same csv file how do I do that?

Ok so I’m working on a pandas program to plot average data on temperature by month but before I can do that I need to figure out how to split the data up into a group by month and show the average temperature. However when I tried to do that it kept showing all of the data instead of splitting it up and showing the average. Can you show me what I’m doing wrong here? I can’t really show the output as it’s basically the whole csv file and that would take up too much space.

import pandas as pd
import matplotlib.pyplot as plt
    
df = pd.read_csv('louisville_weather_data.csv', usecols=['Temperature', 'Wind Speed', 'Precipitation', 'Day', 'Month'])
df.groupby(by='Month')['Temperature'].mean
print(df.groupby)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try:

results = df.groupby(by='Month')['Temperature'].mean()
print(results)
  1. The mean calculation using the groupby is returning something (a pd.Series in this case), not mutating your existing DataFrame, df

  2. Also note that df.groupby(by='Month')['Temperature'].mean without the parentheses, like .mean(), will not call the mean method

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading