Selecting one or more columns with an alias from a table in SQL, translated to Python's pandas.
Selecting a single column with an alias
In SQL:
SELECT
column_1 AS alias_1
FROM
table;
In pandas:
table
.rename({'column_1': 'alias_1'})
['alias_1']
Selecting multiple columns with an alias
In SQL:
SELECT
column_1 AS alias_1,
column_2 AS alias_2
FROM
table;
In pandas:
table
.rename({'column_1': 'alias_1', 'column_2': 'alias_2'})
[['alias_1', 'alias_2']]
Selecting aggregations 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'))