本文整理匯總了Python中pandas.groupby方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.groupby方法的具體用法?Python pandas.groupby怎麽用?Python pandas.groupby使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas
的用法示例。
在下文中一共展示了pandas.groupby方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _plotWeekdayStats
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import groupby [as 別名]
def _plotWeekdayStats(stats, columns, groupBy=True):
dataToPlot = stats.copy()
# Group by weekday and rename date column
if groupBy:
dataToPlot = dataToPlot.groupby(stats['date'].dt.weekday).mean()
dataToPlot = dataToPlot.reset_index().rename(columns={'date':'weekday'})
# change stats from columns to row attribute
dataToPlot = pd.melt(dataToPlot, id_vars=['weekday'], value_vars=columns,
var_name='stats', value_name='val')
# Rename stats and weekdays
dataToPlot['stats'].replace(NAMES, inplace=True)
dataToPlot['weekday'].replace(dayOfWeek, inplace=True)
# Plot
g = sns.factorplot(data=dataToPlot, x="weekday", y="val", col="stats",
order=dayOfWeekOrder, kind="point", sharey=False, col_wrap=3)
g.set_xticklabels(rotation=45)
g.set(xlabel='')
return g
#sns.plt.show()
示例2: test_groupby
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import groupby [as 別名]
def test_groupby(self):
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
pd.groupby(pd.Series([1, 2, 3]), [1, 1, 1])
示例3: _prepareWeekdayByMonthStats
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import groupby [as 別名]
def _prepareWeekdayByMonthStats(stats):
# Add day and month columns, and groupby
stats = stats.copy()
stats['day'] = stats['date'].dt.weekday
stats['month'] = stats['date'].dt.month
dataToPlot = stats.groupby(['day', 'month']).mean()
dataToPlot = dataToPlot.reset_index()
dataToPlot['day'].replace(dayOfWeek, inplace=True)
dataToPlot['month'].replace(months, inplace=True)
return dataToPlot
# def plotWeekdayStats(stats, columns):
# """
# Plot aggregated (mean) stats by dayOfWeek
# :param stats: data to plot
# :param columns: columns from stats to plot
# """
# MEASURE_NAME = 'weekday'
# dayOfWeek={0:'Mon', 1:'Tue', 2:'Wed', 3:'Thur', 4:'Fri', 5:'Sat', 6:'Sun'}
# order = ['Mon','Tue','Wed','Thur','Fri','Sat','Sun']
# stats[MEASURE_NAME] = stats[MEASURE_NAME].map(dayOfWeek)
#
# f, axes = getAxes(2,2)
# for i, c in enumerate(columns):
# if c in NAMES:
# c = NAMES[c]
# g = sns.barplot(x=MEASURE_NAME, y=c, data=stats, order=order, ax=axes[i])
# g.set_xlabel('')
# sns.plt.show()
# #plot(stats, columns, MEASURE_NAME, 2, 3, order=order)
示例4: _prepareYearAndMonthStats
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import groupby [as 別名]
def _prepareYearAndMonthStats(stats, columns):
# Group by month and change stats from columns to row attribute
dataToPlot = stats.groupby(stats['date'].dt.to_period("M")).mean()
dataToPlot = pd.melt(dataToPlot.reset_index(), id_vars=['date'], value_vars=columns,
var_name='stats', value_name='val')
# Rename stats
dataToPlot['stats'].replace(NAMES, inplace=True)
return dataToPlot
示例5: plotDailyStatsHb
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import groupby [as 別名]
def plotDailyStatsHb(data):
ax = data.groupby(data[hbStats.NAME_DT_COL].dt.date).mean().plot()
#data.groupby(data[hbStats.NAME_DT_COL].dt.date).mean().rolling(30).mean().plot(ax=ax)
sns.plt.show()
示例6: _plotMonthlyStats
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import groupby [as 別名]
def _plotMonthlyStats(stats, columns, groupBy=True):
dataToPlot = stats.copy()
# Group by month and rename date column
if groupBy:
dataToPlot = dataToPlot.groupby(stats['date'].dt.month).mean()
dataToPlot = dataToPlot.reset_index().rename(columns={'date': 'month'})
# change stats from columns to row attribute
dataToPlot = pd.melt(dataToPlot, id_vars=['month'], value_vars=columns,
var_name='stats', value_name='val')
# Rename stats and weekdays
dataToPlot['stats'].replace(NAMES, inplace=True)
dataToPlot['month'].replace(months, inplace=True)
order = [m for m in monthsOrder if m in dataToPlot['month'].unique()]
# Plot
g = sns.factorplot(data=dataToPlot, x="month", y="val", col="stats", order=order, kind="bar", sharey=False)
g.set_xticklabels(rotation=45)
g.set(xlabel='')
return g
#sns.plt.show()
# def _plotMonthlyStats(stats, columns):
# """
# Plot aggregated (mean) stats by month
# :param stats: data to plot
# :param columns: columns from stats to plot
# """
# MEASURE_NAME = 'month'
# months={1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug',
# 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'}
# order = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
# stats[MEASURE_NAME] = stats[MEASURE_NAME].map(months)
#
# order = [m for m in order if m in stats[MEASURE_NAME].unique()]
#
# f, axes = getAxes(2,2)
# for i, c in enumerate(columns):
# if c in NAMES:
# c = NAMES[c]
# g = sns.barplot(x=MEASURE_NAME, y=c, data=stats, order=order, ax=axes[i])
# g.set_xlabel('')
# sns.plt.show()