Pandas: Getting “TypeError: only integer scalar arrays can be converted to a scalar index” while trying to merge data frames

前端 未结 2 1734
天涯浪人
天涯浪人 2021-01-05 12:04

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         


        
2条回答
  •  再見小時候
    2021-01-05 12:12

    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
    

提交回复
热议问题