pandas中aggregation无法运行
import numpy as npimport pandas as pd
from pandas import Series, DataFrame
!dir C:\Users\Thinkpad\coding-156-master\coding-156\homework
df = pd.read_csv(r'C:\Users\Thinkpad\coding-156-master\coding-156\homework\city_weather.csv')
g = df.groupby('city')
def foo(attr):
return attr.max() - attr.min()
g.agg(foo)
出现如下报错:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In, line 1
----> 1 g.agg(foo)
File ~\anaconda3\Lib\site-packages\pandas\core\groupby\generic.py:1482, in DataFrameGroupBy.aggregate(self, func, engine, engine_kwargs, *args, **kwargs)
1480 gba = GroupByApply(self, , args=(), kwargs={})
1481 try:
-> 1482 result = gba.agg()
1484 except ValueError as err:
1485 if "No objects to concatenate" not in str(err):
File ~\anaconda3\Lib\site-packages\pandas\core\apply.py:193, in Apply.agg(self)
190 return self.agg_dict_like()
191 elif is_list_like(func):
192 # we require a list, but not a 'str'
--> 193 return self.agg_list_like()
195 if callable(func):
196 f = com.get_cython_func(func)
File ~\anaconda3\Lib\site-packages\pandas\core\apply.py:326, in Apply.agg_list_like(self)
318 def agg_list_like(self) -> DataFrame | Series:
319 """
320 Compute aggregation in the case of a list-like argument.
321
(...)
324 Result of aggregation.
325 """
--> 326 return self.agg_or_apply_list_like(op_name="agg")
File ~\anaconda3\Lib\site-packages\pandas\core\apply.py:1571, in GroupByApply.agg_or_apply_list_like(self, op_name)
1566 # Only set as_index=True on groupby objects, not Window or Resample
1567 # that inherit from this class.
1568 with com.temp_setattr(
1569 obj, "as_index", True, condition=hasattr(obj, "as_index")
1570 ):
-> 1571 keys, results = self.compute_list_like(op_name, selected_obj, kwargs)
1572 result = self.wrap_results_list_like(keys, results)
1573 return result
File ~\anaconda3\Lib\site-packages\pandas\core\apply.py:385, in Apply.compute_list_like(self, op_name, selected_obj, kwargs)
379 colg = obj._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index])
380 args = (
381
382 if include_axis(op_name, colg)
383 else self.args
384 )
--> 385 new_res = getattr(colg, op_name)(func, *args, **kwargs)
386 results.append(new_res)
387 indices.append(index)
File ~\anaconda3\Lib\site-packages\pandas\core\groupby\generic.py:257, in SeriesGroupBy.aggregate(self, func, engine, engine_kwargs, *args, **kwargs)
255 kwargs["engine"] = engine
256 kwargs["engine_kwargs"] = engine_kwargs
--> 257 ret = self._aggregate_multiple_funcs(func, *args, **kwargs)
258 if relabeling:
259 # columns is not narrowed by mypy from relabeling flag
260 assert columns is not None# for mypy
File ~\anaconda3\Lib\site-packages\pandas\core\groupby\generic.py:362, in SeriesGroupBy._aggregate_multiple_funcs(self, arg, *args, **kwargs)
360 for idx, (name, func) in enumerate(arg):
361 key = base.OutputKey(label=name, position=idx)
--> 362 results = self.aggregate(func, *args, **kwargs)
364 if any(isinstance(x, DataFrame) for x in results.values()):
365 from pandas import concat
File ~\anaconda3\Lib\site-packages\pandas\core\groupby\generic.py:294, in SeriesGroupBy.aggregate(self, func, engine, engine_kwargs, *args, **kwargs)
291 return self._python_agg_general(func, *args, **kwargs)
293 try:
--> 294 return self._python_agg_general(func, *args, **kwargs)
295 except KeyError:
296 # KeyError raised in test_groupby.test_basic is bc the func does
297 #a dictionary lookup on group.name, but group name is not
298 #pinned in _python_agg_general, only in _aggregate_named
299 result = self._aggregate_named(func, *args, **kwargs)
File ~\anaconda3\Lib\site-packages\pandas\core\groupby\generic.py:327, in SeriesGroupBy._python_agg_general(self, func, *args, **kwargs)
324 f = lambda x: func(x, *args, **kwargs)
326 obj = self._obj_with_exclusions
--> 327 result = self._grouper.agg_series(obj, f)
328 res = obj._constructor(result, name=obj.name)
329 return self._wrap_aggregated_output(res)
File ~\anaconda3\Lib\site-packages\pandas\core\groupby\ops.py:864, in BaseGrouper.agg_series(self, obj, func, preserve_dtype)
857 if not isinstance(obj._values, np.ndarray):
858 # we can preserve a little bit more aggressively with EA dtype
859 #because maybe_cast_pointwise_result will do a try/except
860 #with _from_sequence.NB we are assuming here that _from_sequence
861 #is sufficiently strict that it casts appropriately.
862 preserve_dtype = True
--> 864 result = self._aggregate_series_pure_python(obj, func)
866 npvalues = lib.maybe_convert_objects(result, try_float=False)
867 if preserve_dtype:
File ~\anaconda3\Lib\site-packages\pandas\core\groupby\ops.py:885, in BaseGrouper._aggregate_series_pure_python(self, obj, func)
882 splitter = self._get_splitter(obj, axis=0)
884 for i, group in enumerate(splitter):
--> 885 res = func(group)
886 res = extract_result(res)
888 if not initialized:
889 # We only do this validation on the first iteration
File ~\anaconda3\Lib\site-packages\pandas\core\groupby\generic.py:324, in SeriesGroupBy._python_agg_general.<locals>.<lambda>(x)
322 alias = com._builtin_table_alias
323 warn_alias_replacement(self, orig_func, alias)
--> 324 f = lambda x: func(x, *args, **kwargs)
326 obj = self._obj_with_exclusions
327 result = self._grouper.agg_series(obj, f)
Cell In, line 2, in foo(attr)
1 def foo(attr):
----> 2 return attr.max() - attr.min()
TypeError: unsupported operand type(s) for -: 'str' and 'str' 你那个感叹号 是什么东东python 有这个语法吗 wp231957 发表于 2024-7-27 15:01
你那个感叹号 是什么东东python 有这个语法吗
jupyternotebook环境下查找指定文件夹内容的语法
页:
[1]