Pandas FutureWarning: 使用多个键索引将被弃用,请改用列表
Pandas FutureWarning: 使用多个键索引将被弃用,请改用列表
在使用 Pandas 的 groupby 函数时,你可能会遇到以下 FutureWarning:
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.
print(df_pk.groupby('Type 1')['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed'].sum())
警告原因:
这个警告是因为你在使用 groupby 函数时,将多个列名作为单独的参数传入,例如 groupby('Type 1')['HP', 'Attack', 'Defense']。Pandas 会将这些列名隐式地转换为一个元组,这在未来的版本中将被弃用。
解决方法:
为了避免这个警告,建议使用一个列表来传递多个列名给 groupby 函数。
示例:
# 错误示范 (引发 FutureWarning)
print(df_pk.groupby('Type 1')['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed'].sum())
# 正确示范
print(df_pk.groupby('Type 1')[['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']].sum())
通过将需要进行分组的列名放在一个列表中,可以避免 FutureWarning,并使你的代码与未来的 Pandas 版本兼容。
原文地址: https://www.cveoy.top/t/topic/futw 著作权归作者所有。请勿转载和采集!