简体   繁体   中英

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

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

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

tmp.rename(columns={'Locode':'POR', 'Port Name':'POR_PORT'}, inplace=True)替换了代码tmp.columns = [['POR','POR_PORT']]并且它工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM