有没有办法对2个数据帧进行分组,所以如果我有
private bool on = true;
private void button11_Click(object sender, EventArgs e)
{
if (on == true)
{
button11.Text = "Off";
button11.BackColor = Color.LightGray;
on = false;
}
else
{
button11.Text = "On";
button11.BackColor = Color.DimGray;
on = true;
}
}
(注意:“|”用于分隔2 df)
我会得到
index col1 col2 | index col1 col2
0 AA 12 | 0 AA 12
1 AB 13 | 1 AB 13
2 AC 14 | 2 AD 15
谢谢!
答案 0 :(得分:3)
import pandas as pd
df1 = pd.DataFrame({'col1':['AA','AB','AC'], 'col2':[12,13,14]})
df2 = pd.DataFrame({'col1':['AA','AB','AD'], 'col2':[12,13,15]})
现在,您可以使用concat
来堆叠它们并group
它们:
df = pd.concat( [df1, df2],axis=0,ignore_index=True)
df = df.groupby('col1').sum().reset_index()
print(df)
输出:
col1 col2
0 AA 24
1 AB 26
2 AC 14
3 AD 15
答案 1 :(得分:1)
您可以将DF2附加到DF1然后再分组:
import pandas as pd
df1=pd.DataFrame({'col1':['AA', 'AB','AC'], 'col2': [12,13,14]})
df2=pd.DataFrame({'col1':['AA', 'AB','AD'], 'col2': [12,13,15]})
df1=df1.append(df2)
df1.groupby('col1').sum()