我有一个csv文件,如下所示
Hour,L,Dr,Tag,Code,Vge
0,L5,XI,PS,4R,15
0,L3,St,sst,4R,17
5,L5,XI,PS,4R,12
2,L0,St,v2T,4R,11
8,L2,TI,sst,4R,8
12,L5,XI,PS,4R,18
2,L2,St,PS,4R,9
12,L3,XI,sst,4R,16
我在ipython
笔记本中执行以下脚本。
In[1]
import pandas as pd
In[2]
df = pd.read_csv('/python/concepts/pandas/in.csv')
In[3]
df.head(n=9)
Out[1]:
Hour L Dr Tag Code Vge
0 0 L5 XI PS 4R 15
1 0 L3 St sst 4R 17
2 5 L5 XI PS 4R 12
3 2 L0 St v2T 4R 11
4 8 L2 TI sst 4R 8
5 12 L5 XI PS 4R 18
6 2 L2 St PS 4R 9
7 12 L3 XI sst 4R 16
In[4]
df.groupby(('Hour'))['Vge'].aggregate(np.sum)
Out[2]:
Hour
0 32
2 20
5 12
8 8
12 34
Name: Vge, dtype: int64
现在我编写一个列表操作square_list
。
In[4]
newlist = []
In[5]
def square_list(x):
for item in x:
newlist.append(item**item)
return newlist
In [44]: df.groupby(('Hour'))['Vge'].aggregate(square_list)
Out[44]:
Hour
0 [437893890380859375, -2863221430593058543, 437...
2 [437893890380859375, -2863221430593058543, 437...
5 [437893890380859375, -2863221430593058543, 437...
8 [437893890380859375, -2863221430593058543, 437...
12 [437893890380859375, -2863221430593058543, 437...
Name: Vge, dtype: object
输出看起来很奇怪。我所期待的是第一个输出中项目的squares
。
如果我使用
df.groupby(('Hour'))['Vge'].aggregate(lambda x: x ** x)
我收到以下错误。
ValueError Traceback (most recent call last)
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in agg_series(self, obj, func)
1632 try:
-> 1633 return self._aggregate_series_fast(obj, func)
1634 except Exception:
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in _aggregate_series_fast(self, obj, func)
1651 dummy)
-> 1652 result, counts = grouper.get_result()
1653 return result, counts
pandas/src/reduce.pyx in pandas.lib.SeriesGrouper.get_result (pandas/lib.c:38634)()
pandas/src/reduce.pyx in pandas.lib.SeriesGrouper.get_result (pandas/lib.c:38503)()
pandas/src/reduce.pyx in pandas.lib._get_result_array (pandas/lib.c:32023)()
ValueError: function does not reduce
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in aggregate(self, func_or_funcs, *args, **kwargs)
2339 try:
-> 2340 return self._python_agg_general(func_or_funcs, *args, **kwargs)
2341 except Exception:
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in _python_agg_general(self, func, *args, **kwargs)
1167 try:
-> 1168 result, counts = self.grouper.agg_series(obj, f)
1169 output[name] = self._try_cast(result, obj)
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in agg_series(self, obj, func)
1634 except Exception:
-> 1635 return self._aggregate_series_pure_python(obj, func)
1636
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in _aggregate_series_pure_python(self, obj, func)
1668 isinstance(res, list)):
-> 1669 raise ValueError('Function does not reduce')
1670 result = np.empty(ngroups, dtype='O')
ValueError: Function does not reduce
During handling of the above exception, another exception occurred:
Exception Traceback (most recent call last)
<ipython-input-47-874cf4c23d53> in <module>()
----> 1 df.groupby(('Hour'))['Vge'].aggregate(lambda x : x**x)
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in aggregate(self, func_or_funcs, *args, **kwargs)
2340 return self._python_agg_general(func_or_funcs, *args, **kwargs)
2341 except Exception:
-> 2342 result = self._aggregate_named(func_or_funcs, *args, **kwargs)
2343
2344 index = Index(sorted(result), name=self.grouper.names[0])
/Applications/anaconda/lib/python3.5/site-packages/pandas/core/groupby.py in _aggregate_named(self, func, *args, **kwargs)
2429 output = func(group, *args, **kwargs)
2430 if isinstance(output, (Series, Index, np.ndarray)):
-> 2431 raise Exception('Must produce aggregated value')
2432 result[name] = self._try_cast(output, group)
2433
Exception: Must produce aggregated value
答案 0 :(得分:1)
您是否正在仔细阅读错误?它说功能不会降低。请花几分钟时间来正确定义您想要的内容。这也是你的with open("Datalog.txt", "r") as file:
result = [[float(x) for x in line.split(",")] for line in file]
for i in range (len(result[0])):
for j in range (len(result)):
print (result[j][i])
print (min(result[0][i]))
函数的确切问题,它返回一个列表,而不是列表元素的总和。它没有减少。
如果你想要简单的总和:
square_list()
如果要平方列中的所有元素:
df.groupby('Hour')['Vge'].sum()
如果你想要组的平方和:
df['Vge_squared'] = df['Vge']**2
或者,
df.groupby('Hour')['Vge_squared'].sum()
或者,
def square_list(x):
x = numpy.array(x)
return numpy.sum(numpy.multiply(x,x))
df.groupby('Hour')['Vge'].aggregate(square_list)
希望这有帮助。
答案 1 :(得分:1)
首先,第一个输出是“按预期”,因为对square_list
的每次调用都附加到全局 newlist
。
您可以在每次通话时创建列表:
def square_list(x):
newlist = []
for item in x:
newlist.append(item**item)
return newlist
In [11]: df.groupby(('Hour'))['Vge'].aggregate(square_list)
Out[11]:
Hour
0 [437893890380859375, -2863221430593058543]
2 [285311670611, 387420489]
5 [8916100448256]
8 [16777216]
12 [-497033925936021504, 0]
dtype: object
但我怀疑这不是你想要的。
错误消息非常准确:“必须产生聚合值”。目前你的lambda没有返回单个值。
也许你想要总和:
In [21]: df.groupby(('Hour'))['Vge'].aggregate(lambda x: (x ** x).sum())
Out[21]:
Hour
0 -8785478146473916416
2 285699091100
5 8916100448256
8 16777216
12 0
Name: Vge, dtype: int64
注意:为方块创建一个虚拟列可能会更快,然后为“干净”组创建总和。