Skip to content

常见问题

DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass include_groups=False to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.

python3.12 + openpyxl 3.1.2 + pandas 2.2.2

要对如下数据进行分组处理:

城市月份金额
北京222
北京435
上海232
上海216

代码运行有warning提示:

python
import pandas as pd

data = pd.read_excel('./data.xlsx')
def func(data):
    data['环比'] = data['金额'] - data['金额'].shift(1)
    return data

data2 = data.sort_values(['城市', '月份']).groupby("城市").apply(func)
print(data2)
"""
DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.
  data2 = data.sort_values(['城市', '月份']).groupby("城市").apply(func)
"""
python
import pandas as pd

data = pd.read_excel('./data.xlsx')
def func(data):
    data['环比'] = data['金额'] - data['金额'].shift(1)
    return data

# 添加include_groups=False解决warning问题
data2 = data.sort_values(['城市', '月份']).groupby("城市").apply(func, include_groups=False)
print(data2)