假设我有一个包含 -
的Pandas DataFrame<pre>
<table>
<tr>
<th scope='column'>Fruit</ th>
<th scope='column'>Address</ th>
<th scope='column'>Quantity</ th>
</ tr>
<br>
<tr>
<td>Banana</ td>
<td>Baker Street</ td>
<td>10</ td>
</ tr>
<br>
<tr>
<td>Banana</ td>
<td>Baker Street</ td>
<td>20</ td>
</ tr>
<br>
<tr>
<td>Banana</ td>
<td>Nautilus</ td>
<td>10</ td>
</ tr>
</ table>
</ pre>
我想要一个包含 -
的最终数据框<pre>
<table>
<tr>
<th scope='column'>Fruit</ th>
<th scope='column'>Address</ th>
<th scope='column'>Quantity</ th>
</ tr>
<br>
<tr>
<td>Banana</ td>
<td>Baker Street</ td>
<td>30</ td>
</ tr>
<br>
<tr>
<td>Banana</ td>
<td>Nautilus</ td>
<td>10</ td>
</ tr>
</ table>
</ pre>
有简洁的方法吗?从本质上讲,我想在最终的唯一列上总结一个包含所有常见列的表。
答案 0 :(得分:1)
我认为你正在寻找群数,即
如果你有像
这样的数据框columns = ['Fruit','Address','Quality']
df = pd.DataFrame([['Banana','Baker Street',10],['Banana','Baker Street',20],['Banana','Nautilus',10]],columns = columns)
Fruit Address Quality 0 Banana Baker Street 10 1 Banana Baker Street 20 2 Banana Nautilus 10
与reset_index合并的Groupby将给出
new_df = df.groupby(['Fruit','Address'])['Quality'].sum().reset_index()
Fruit Address Quality 0 Banana Baker Street 30 1 Banana Nautilus 10
您也可以使用as_index = False
即
new_df = df.groupby(['Fruit','Address'],as_index=False)['Quality'].sum()
希望有所帮助