上面的例子有些琐碎。我正在尝试以更复杂的代码删除警告消息。如何转换下面的代码以使用列列表来消除警告?
# Standard imports
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
# Ensure plots are displayed inline
%matplotlib inline
#%matplotlib notebook
# Read in some data to show some real world exampled
df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true")
# Examine the dataframe
df.head()
# Summarize the data by customer and get the top 10 customers. Also, clean up the column names for consistency
top_10 = (df.groupby('name')['ext price', 'quantity']
.agg({'ext price': 'sum', 'quantity': 'count'})
.sort_values(by='ext price', ascending=False))[:10].reset_index()
`C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.
"""Entry point for launching an IPython kernel.`
# Output the top 10
top_10
我尝试了这段代码,但我确实出错了,而不仅仅是警告:
top_10 = (df.groupby('name')['ext price', 'quantity']
.agg([['ext price', 'sum'], ['quantity', 'count']])
.sort_values(by='ext price', ascending=False))[:10].reset_index()
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1:
FutureWarning:使用多个键进行索引(隐式转换为
元组键)将被弃用,请改用列表。 “““入口
启动 IPython 内核的要点。
----------------------------------- ---------------------------- TypeError Traceback(最近一次调用
最后)~\AppData\Local\Temp/ipykernel_3644/534927805.py 在
1 top_10 = (df.groupby('name')['ext price', 'quantity']
----> 2 .agg([['ext price', 'sum'], ['quantity', 'count']])
3 .sort_values(by='ext price', ascending=False))[:10].reset_index()
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\groupby\generic.py
汇总(自我,函数,引擎,engine_kwargs,*args,**kwargs)
940 返回 self.obj._constructor(结果,索引 = 索引,列 = 数据列)
941
--> 942 重新标记,函数,列,顺序 = 重构函数(函数,**kwargs)
第943章
第944章
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\aggregation.py
在reconstruct_func(func, **kwargs)
88
89 如果不重新标记:
---> 90 如果 isinstance(func, list) 和 len(func) > len(set(func)):
91
92 # GH 28426 如果使用了重复的函数名并且会引发错误
TypeError: unhashable type: 'list'