3 examples of 'pandas agg' in Python

Every line of 'pandas agg' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
291def runAgg(spark, df):
292 df.agg(max("amount"), min("price")).show()
293 df.agg({"amount": "max", "price": "min"}).show()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

237def agg_to_year(self, df, func='sum'):
238 """
239 Aggregate a DataFrame (cells x months) to (cells x years).
240
241 The groups are defined by an array the length of the number of columns,
242 where identical values are grouped. Integer division by 12 on the
243 column index gives sequential groups of length 12.
244
245 :param df: DataFrame with months in columns
246 :param func: Function to use for aggregation [default sum]
247 """
248 return df.groupby(np.arange(len(df.columns)) // NMONTHS, axis=1).agg(func)
137def test_aggregate_no_groups():
138 df = pd.DataFrame({
139 'df.g': [0, 0, 1, 1],
140 'df.a': [4, 5, 6, 7],
141 })
142
143 def _scalar_df(values):
144 return pd.DataFrame(values, index=[0])
145
146 def perform(q):
147 node = Aggregate(Literal(df), [DerivedColumn.parse(q)])
148
149 ex = PandasExecutor()
150 return ex.evaluate(node, None)
151
152 pdt.assert_frame_equal(perform('SUM(a) as c'), _scalar_df({'$0.c': 22}))
153 pdt.assert_frame_equal(perform('AVG(a) as c'), _scalar_df({'$0.c': 22 / 4.0}))
154 pdt.assert_frame_equal(perform('MIN(a) as c'), _scalar_df({'$0.c': 4}))
155 pdt.assert_frame_equal(perform('MAX(a) as c'), _scalar_df({'$0.c': 7}))
156 pdt.assert_frame_equal(perform('COUNT(a) as c'), _scalar_df({'$0.c': 4}))

Related snippets