Selecting the average of column values from a SQL table, translated to Python's pandas.
Selecting a single column's average value
In SQL:
SELECT
AVG(column_1)
FROM
table;
In pandas:
table['column_1'].mean()
Selecting the average of multiple columns
In SQL:
SELECT
AVG(column_1),
AVG(column_2)
FROM
table;
In pandas:
table
.agg({'column_1': ['mean'], 'column_2': ['mean']})
Selecting the average of columns with an alias
In SQL:
SELECT
column_1,
AVG(column_2) AS agg_alias_1,
AVG(column_3) AS agg_alias_2
FROM
table
GROUP BY
column_1;
In pandas:
table
.groupby('column_1')
.agg(agg_alias_1=('column_2', 'mean'), agg_alias_2=('column_3', 'mean'))