Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我使用了一个简单的“groupby”来压缩 Pandas 数据框中的行:
df = df.groupby(['col1', 'col2', 'col3']).sum()
在新的 DataFrame 'df' 中,'groupby' 函数中使用的三列现在固定在索引中,不再是列索引 0、1 和 2 - 以前的列索引 4 现在是列索引 0。
如何阻止这种情况发生/重新包含三个“groupby”列以及原始数据?
尝试重置索引
df = df.reset_index()
尝试 -
df = df.groupby(['col1', 'col2', 'col3'], as_index = False).sum() #or df = df.groupby(['col1', 'col2', 'col3']).sum().reset_index()