Home > Enterprise >  If I want to split data in one part of a csv file in pandas by another column in the same csv file h
If I want to split data in one part of a csv file in pandas by another column in the same csv file h

Time:11-13

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)

CodePudding user response:

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

  • Related