简体   繁体   English

有没有办法将特定列从 Excel 工作表(例如 sheet_1)复制到 sheet_2 中的另一列? 使用 Python

[英]Is there any way to copy a particular column from excel sheet (say sheet_1) to the other column which is in sheet_2? Using Python

Please find the code below:请在下面找到代码:

import pandas as pd
import csv

# Reading the csv file 
df_new = pd.read_csv('source.csv') 

# saving xlsx file 
GFG = pd.ExcelWriter('source.xlsx') 
df_new.to_excel(GFG, index = False) 

GFG.save() 

# read excel
xl = pd.ExcelFile("source.xlsx")
df = xl.parse("Sheet1")

# get the column you want to copy
column = df["Marks"]

# paste it in the new excel file
with pd.ExcelWriter('Target_excel.xlsx', mode='A') as writer:
    column.to_excel(writer, sheet_name= "new sheet name", index = False)

writer.close()

In this code, it is replacing the existing contents of the target excel file.在这段代码中,它正在替换目标 excel 文件的现有内容。 I want to update a column in sheet 2 without changing other columns.我想在不更改其他列的情况下更新工作表 2 中的列。

Example:例子:

Excel file 1--> column_name = 'Marks' Excel 文件 1--> column_name = 'Marks'

Marks = 10,20,30标记 = 10,20,30

Excel file 2--> there are two columns present in this file Excel 文件 2--> 此文件中有两列

Subject_name = Math, English, Science Subject_name = 数学、英语、科学

Marks = 50, 20, 40分数 = 50, 20, 40

So I want to copy "Marks" column from Excel file 1 and paste it into "Marks" column of Excel file 2(Without changing the data of "Subject" column)所以我想从 Excel 文件 1 复制“标记”列并将其粘贴到 Excel 文件 2 的“标记”列中(不更改“主题”列的数据)

import pandas as pd
import openpyxl as pxl

def get_col_idx(worksheet, col_name):
    return next((i for i, col in enumerate(worksheet.iter_cols(1, worksheet.max_column)) if col[0].value == col_name), -1)

### ----- 0. csv -> xlsx (no change from your code)

df_new = pd.read_csv("source.csv")

GFG = pd.ExcelWriter("source.xlsx")
df_new.to_excel(GFG, index=False)
GFG.save()

### ----- 1. getting data to copy

# open file and get sheet of interest
source_workbook = pxl.load_workbook("source.xlsx")
source_sheet = source_workbook["Sheet1"]

# get "Marks" column index
col_idx = get_col_idx(source_sheet, "Marks")

# get contents in each cell
col_contents = [row[col_idx].value for row in source_sheet.iter_rows(min_row=2)]

### ----- 2. copy contents to target excel file

target_workbook = pxl.load_workbook("Target_excel.xlsx")
target_sheet = target_workbook["new sheet name"]

col_idx = get_col_idx(target_sheet, "Marks")

for i, value in enumerate(col_contents):
    cell = target_sheet.cell(row=i+2, column=col_idx+1)
    cell.value = value

target_workbook.save("Target_excel.xlsx")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python 将多个列从 excel 表的 file1 复制到 file2 的其他列/行 其他 excel 表,使用 pandas 或其他任何东西 - copy multiple columns from file1 of an excel sheet to other column/row of file2 other excel sheet using python , use pandas or anything else 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook 在工作表中添加一列,然后使用python pandas或任何其他更好的库在整个列中应用此excel公式 - Add a column to sheet and apply this excel formula in whole column using python pandas or any other better library 将列复制到Python中的另一个工作表 - Copy column to another sheet in Python 我想将特定列保存到 python 中的新 excel 表中 - i want to save a particular column into a new excel sheet in python 通过从其他工作表中查找分母值,用 Excel 工作表中列的数据的除法填充新列 - fill a new column with the division of data of a column in a Excel Sheet by looking up the denominator value from other Sheet 在python中逐表和逐列附加多个excel文件 - Append multiple excel files sheet by sheet & column by column in python 使用python反转Excel工作表中的2列,其中一列具有单个变量,另一列具有列表 - Reversing 2 columns in an excel sheet with one column having a single variable and the other column having a list using python 使用python中的URL创建数据的任何方法 - Any way to create an excel sheet with data from a url with python 如何使用 python 熊猫仅打印到 excel 工作表中的一列 - How to print to only one column in an excel sheet using python panda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM