python - Pandas :尝试合并数据帧时获取 "TypeError: only integer scalar arrays can be converted to a scalar index"

标签 python pandas dataframe

重命名DataFrame的列后,合并新列时出现错误:

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

最佳答案

重命名列时,请使用DataFrame.columns = [list],而不要使用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

关于python - Pandas :尝试合并数据帧时获取 "TypeError: only integer scalar arrays can be converted to a scalar index",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59879577/

相关文章:

python - 1 个输入分隔中的 3 个值(数字)。 python 3

python - 具有多索引数据帧的 Pandas eval

python - PyCharm 警告 "End of statement expected"类型提示内部

python - 使用字符串访问数据存储列

Python Pandas 数据框查找缺失值

python - 使用 Python 中 ma numpy 中的 fiil_value 将屏蔽值 (--) 替换为 Null 或 None 值

python - 从字典列表创建 Pandas MultiIndex 的最佳方法是什么?

r - 根据字符乘以数据框中的单元格

python - Numpy 列减法;从 (k,) 数组中的值减去 (k,n) 数组

python - 通过比较 Pandas 中前 n 行来获取列的最小值