Fix Python – Multiple aggregations of the same column using pandas GroupBy.agg()

Is there a pandas built-in way to apply two different aggregating functions f1, f2 to the same column df[“returns”], without having to call agg() multiple times?
Example dataframe:
import pandas as pd
import datetime as dt
import numpy as np

pd.np.random.seed(0)
df = pd.DataFrame({
“date” : [dt.date(2012, x, 1) for x in range(1, 11)]….

Fix Python – How can I pivot a dataframe?

What is pivot?
How do I pivot?
Is this a pivot?
Long format to wide format?

I’ve seen a lot of questions that ask about pivot tables. Even if they don’t know that they are asking about pivot tables, they usually are. It is virtually impossible to write a canonical question and answer that encompasses all aspects of pivoting…
… But I’m goin….

Fix Python – Converting a Pandas GroupBy output from Series to DataFrame

I’m starting with input data like this
df1 = pandas.DataFrame( {
“Name” : [“Alice”, “Bob”, “Mallory”, “Mallory”, “Bob” , “Mallory”] ,
“City” : [“Seattle”, “Seattle”, “Portland”, “Seattle”, “Seattle”, “Portland”] } )

Which when printed appears as this:
City Name
0 Seattle Alice
1 Seattle Bob
2 Portland Mallory
3 Se….

Fix Python – Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

I have a data frame df and I use several columns from it to groupby:
df[‘col1′,’col2′,’col3′,’col4’].groupby([‘col1′,’col2’]).mean()

In the above way I almost get the table (data frame) that I need. What is missing is an additional column that contains number of rows in each group. In other words, I have mean but I also would like to know how man….