python – Merge two DataFrame tables, aggregate data using pandas


Wrote the code:

array = pd.DataFrame(np.object, index=[], columns=[])
array = array.append({'Project':, 'Summary':, \
                  'Key':, 'Assignee': m, 'Points': s}, \
array1 = pd.read_excel('ProjectCostUCP.xlsx', 'Лист1')
result = array[['Project', 'Assignee', 'Points']].groupby(['Project', 
result = result.merge(array1, on=['Project'])
result['Value'] = result.Points * result.Price
writer = pd.ExcelWriter('third.xlsx')
array.to_excel(writer, "June")
result.to_excel(writer, "June_agregation")

It is necessary to unite two tables – result and array1 by column name – 'Project'. But, as the line print(result.columns.tolist()) shows, there is only one column in the result table – 'Points'. Although before the groupby() operation there was also a 'Project' column. groupby() put 'Project' in some kind of MultiIndex from which I can't pull data. Please help me how to fix the code so that it works?
The end result of the code should be a table:

Project    Assignee Price   Points  Value
srnd-demo   Serhii  5,5     23      126,5

and after executing the line

result = array[['Project', 'Assignee','Points']].groupby(['Project', 

resulting in a table:

Project    Assignee 
srnd-demo   Serhii   23

Points – turned out to be the name of Columns, and Project Assignee got into the MultiIndex type, which is not possible to work with (namely, pull data from it), the array1 table looks like:

Project    Price
srnd-demo  5.5

where Project Price is the name of the columns. You need to join the result and array1 tables by the name columns Project, then multiply Price by Points and place the value in the Value column. Usage


resulted in the creation of a table:

   Project     Assignee  Price_x  Points  Price_y
0  srnd-demo   Serhii      5.5    23.0      5.5

How will achieve, that all the same Price in the table was one?


Use the .reset_index() method to turn all index/multi-index columns into normal columns:

In [245]: result
Project   Assignee
srnd-demo Serhii        23

In [246]: array1
     Project  Price
0  srnd-demo    5.5

In [247]: result.reset_index().merge(array1)
     Project Assignee  Points  Price
0  srnd-demo   Serhii      23    5.5


If the DataFrame's contains columns with the same names that do not participate in the union (that is, they were not specified as the on , left_on , right_on ), then as a result these columns will have suffixes (default: '_x' – for the left DF and '_y' for right).


In [259]: result
                    Points  Price
Project   Assignee
srnd-demo Serhii        23    5.5

In [260]: array1
     Project  Price
0  srnd-demo    5.5

In [257]: result.reset_index().merge(array1, on='Project')
     Project Assignee  Points  Price_x  Price_y
0  srnd-demo   Serhii      23      5.5      5.5

get rid of duplicate columns in one of the dfs:

In [258]: result.reset_index().merge(array1.drop(['Price'], axis=1), on='Project')
     Project Assignee  Points  Price
0  srnd-demo   Serhii      23    5.5
Scroll to Top