首页 > 解决方案 > 我正在尝试用一个公共列映射两个 Excel 工作表,我收到错误声明为属性错误

问题描述

import pandas as pd

#file1 = "mapp1.xlsx"
#file2 = "mapp2.xlsx"

file1 = "C:\\Users\\Sudharshan\\Desktop\\file_map\\mapp1.xlsx"
file2 = "C:\\Users\\Sudharshan\\Desktop\\file_map\\mapp2.xlsx"

df1 = pd.read_excel(file1)
df2 = pd.read_excel(file2)

marge = pd.marge(df1, df2, on="Name")

print(marge)

请在此处提供帮助,执行时出现此错误:

玛吉不是熊猫的属性

我正在尝试使用 Jupyter notebook pycharm 和 google collab

AttributeError
Traceback(最近一次调用最后一次)在

10 df2 = pd.read_excel(file2)
11
---> 12 marge = pd.marge(df1, df2, on="Name")
13
14 print(marge)

~\anaconda3\lib\site-packages\pandas_init _.py in getattr (name)
256 return _SparseArray
257
--> 258 raise AttributeError(f"module 'pandas' has no attribute '{name}'")
259
260

AttributeError:模块'pandas'没有属性'marge'

标签: pythonexcel

解决方案


您的代码中有错字。我认为应该是 pd.merge 而不是 pd.marge

也用于组合两个excel表列...

尝试这个:

假设您的工作簿 1 或 excel 文件 1 有四列,分别名为“Locations”、“Employees”、“Revenue”和“Profit”。你的 excel 文件 2 也有所有这些列和一些额外的列。现在您要合并 output.xlsx 中两个文件中的这四列

所以,你可以这样做:

import pandas as pd

excel1 = 'workbook1.xlsx'
excel2 = 'workbook2.xlsx'

df1 = pd.read_excel(excel1)
df2 = pd.read_excel(excel2)

values1 = df1['Locations','Employees','Revenue','Profit']
values2 = df2['Locations','Employees','Revenue','Profit']

dataframes = [values1, values2]

join = pd.concat(dataframes)

join.to_excel("output.xlsx")

推荐阅读