问题描述
我试图在单个Id
列上保留多个熊猫数据框的联接,但是当我尝试合并时,我得到警告:
I'm trying to left join multiple pandas dataframes on a single Id
column, but when I attempt the merge I get warning:
我认为可能是因为我的数据框具有由groupby
语句产生的偏移列,但是我很可能是错误的.无论哪种方式,我都无法弄清楚如何拆堆"我的数据框列标题. 此问题的答案均无效.
I think it might be because my dataframes have offset columns resulting from a groupby
statement, but I could very well be wrong. Either way I can't figure out how to "unstack" my dataframe column headers. None of the answers at this question seem to work.
我的groupby
代码:
step1 = pd.DataFrame(step3.groupby(['Id', 'interestingtabsplittest2__grp'])['applications'].sum())
step1.sort('applications', ascending=False).head(3)
返回:
如何将那些偏移量标头放到顶层?
How to get those offset headers into the top level?
推荐答案
您正在寻找 .reset_index()
.
You're looking for .reset_index()
.
In [11]: df = pd.DataFrame([[2, 3], [5, 6]], pd.Index([1, 4], name="A"), columns=["B", "C"])
In [12]: df
Out[12]:
B C
A
1 2 3
4 5 6
In [13]: df.reset_index()
Out[13]:
A B C
0 1 2 3
1 4 5 6
注意:进行分组时,可以通过使用as_index=False
来避免此步骤.
Note: That you can avoid this step by using as_index=False
when doing the groupby.
step1 = step3.groupby(['Id', 'interestingtabsplittest2__grp'], as_index=False)['applications'].sum()
这篇关于groupby之后,如何展平列标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!