Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pandas merge question
#1
I hope you are all having a good day. I want to know why my merge below is returning the key error that is stated below my code. Any help would be appreciated.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

dict1={'eggs':(1,7,44,76,23,56,22,21,20,11,12,81),'month':('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec')}

dict2={'bread':(22,43,17,9,22,16,71,82,24,34,54,45),'months':('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec')}

df1=pd.DataFrame(dict1)

df2=pd.DataFrame(dict2)

df1.columns.values[1]='months'

pd.merge(left=df1,right=df2,on='months')
KeyError: 'months'
Quote
#2
Your example works for me. Perhaps you forgot to run line 15 (that renames month column as months column), so there was no 'months' key in df1 and merge failed?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Python Pandas for loop/while loop question mrashy 1 80 Mar-24-2020, 04:39 AM
Last Post: deanhystad
  Pandas dataframe merge snmmat 1 150 Mar-09-2020, 06:56 PM
Last Post: jefsummers
  Pandas merge csv files karlito 2 284 Dec-16-2019, 10:59 AM
Last Post: karlito
  Counting Criteria in Pandas Question Koenig 1 271 Sep-30-2019, 05:16 AM
Last Post: perfringo
  Function question using Pandas smw10c 7 3,049 Feb-12-2019, 06:52 PM
Last Post: Nathandsn
  Simple pandas dataframe question popohoma 1 720 Jan-03-2019, 05:00 PM
Last Post: ashlardev
  Why can't I merge pandas dataframes learnpython2018 2 1,820 Sep-23-2018, 05:53 PM
Last Post: learnpython2018
  question on pandas datareader kit12_31 3 4,869 Feb-05-2018, 11:55 PM
Last Post: snippsat
  Newbie question on how to use pandas.rolling_mean zydjohn 5 8,746 Dec-09-2017, 08:42 PM
Last Post: j.crater
  Pandas question takaa 3 1,528 Dec-05-2017, 01:03 PM
Last Post: takaa

Forum Jump:


Users browsing this thread: 1 Guest(s)