5

How I can merge two pandas pivot tables? When I try run my code I have error: keyerror

data_pivot= pandas.DataFrame(data.pivot_table(values = 'NR_ACTIONS', index=["HOUR", "OPID", "NAME"], columns='CONTACTED_PERSON_NEW', aggfunc='sum'))
data_pivot.fillna(0, inplace=True)
data2_pivot= pandas.DataFrame(data2.pivot_table(values = 'AMOUNT_PA', index=["HOUR", "OPID", "NAME"], columns='PA_TYPE', aggfunc='sum'))
data2_pivot.fillna(0, inplace=True)
all_data = pandas.merge(data_pivot, data2_pivot, 'left', on = ["HOUR", "OPID", "NAME"] ) 

2 Answers 2

8

answer for my question is :

data_pivot= pandas.DataFrame(data.pivot_table(values = 'NR_ACTIONS', index=["HOUR", "OPID", "NAME"], columns='CONTACTED_PERSON_NEW', aggfunc='sum'))
data_pivot.fillna(0, inplace=True)
data_pivot.reset_index( inplace=True)
data2_pivot= pandas.DataFrame(data2.pivot_table(values = 'AMOUNT_PA', index=["HOUR", "OPID", "NAME"], columns='PA_TYPE', aggfunc='sum'))
data2_pivot.fillna(0, inplace=True)
data2_pivot.reset_index( inplace=True)
all_data = pandas.merge(data_pivot, data2_pivot, 'left', on = ["HOUR", "OPID", "NAME"] )
Sign up to request clarification or add additional context in comments.

2 Comments

that was great, what does the reset_index do that it fixes the key error? thank you
Had same 'key error' problem after trying to manipulate a df after a pivot_table procedure. df.reset_index(inplace=true) works! Although doing a match function ".isin" comparing two df's resulted in a "NoneType" object error, the solution was to leave out the "inplace=true." So df.reset_index() was the final answer.
0

The best solution here, if you want to keep the index, would be to tell pandas to merge on the index on the left and right tables. By doing this, you don't need to reset the index.

data_pivot= pandas.DataFrame(data.pivot_table(values = 'NR_ACTIONS', index=["HOUR", "OPID", "NAME"], columns='CONTACTED_PERSON_NEW', aggfunc='sum'))
data_pivot.fillna(0, inplace=True)
data2_pivot= pandas.DataFrame(data2.pivot_table(values = 'AMOUNT_PA', index=["HOUR", "OPID", "NAME"], columns='PA_TYPE', aggfunc='sum'))
data2_pivot.fillna(0, inplace=True)

all_data = pandas.merge(data_pivot, data2_pivot, left_index=True,right_index=True )

You can always go to Pandas' doc to see the different arguments

PS: If you use multi index, it works, but you need to be careful, the number of keys in the other DataFrame (either the index or a number of columns) must match the number of levels.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.