11

After renaming a DataFrame's column(s), I get an error when merging on the new column(s):

import pandas as pd

df1 = pd.DataFrame({'a': [1, 2]})
df2 = pd.DataFrame({'b': [3, 1]})

df1.columns = [['b']]

df1.merge(df2, on='b')

TypeError: only integer scalar arrays can be converted to a scalar index

Max Ghenis
  • 14,783
  • 16
  • 84
  • 132
Ilyas
  • 181
  • 1
  • 1
  • 8
  • Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – anky Jan 23 '20 at 13:21
  • what does `df` look like? – hongsy Jan 23 '20 at 14:00
  • i tried the same in my home PC and i didnt face this issue. let me try and do it tomorrow and post it – Ilyas Jan 24 '20 at 07:42
  • Replaced the below code tmp.columns = [['POR','POR_PORT']] with tmp.rename(columns={'Locode':'POR', 'Port Name':'POR_PORT'}, inplace=True) and it worked. Thanks for your time. – Ilyas Jan 25 '20 at 05:37

2 Answers2

23

When renaming columns, use DataFrame.columns = [list], not DataFrame.columns = [[list]]:

df1 = pd.DataFrame({'a': [1, 2]})
df2 = pd.DataFrame({'b': [3, 1]})

df1.columns = ['b']

df1.merge(df2, on='b')
#   b
# 0 1
Max Ghenis
  • 14,783
  • 16
  • 84
  • 132
  • 2
    You have no idea how frustrating this was for like a solid hour. Thank you. this worked. – max Feb 24 '21 at 19:15
6

Replaced the code tmp.columns = [['POR','POR_PORT']] with tmp.rename(columns={'Locode':'POR', 'Port Name':'POR_PORT'}, inplace=True) and it worked.

Ilyas
  • 181
  • 1
  • 1
  • 8