简体   繁体   English

如何将Pandas Series插入现有Excel文件的特定列中(而不从该文件中删除内容)?

[英]How to insert a Pandas Series into a specific column of an existing Excel file (without deleting content from that file)?

I have imported the following data from Excel with pandas: 我已经使用熊猫从Excel导入了以下数据:

import pandas as pd
sht = pd.read_excel(path, 'Table', index_col=None, header=None, usecols = "A:C")
sht.head()

|-------+------------+----------|
| jon   |   tyrion   | daenerys |
| sansa |   cersei   |  rhaegar |
| arya  |   jaime    |        0 |
| bran  |   tywin    |        0 |
| robb  |   0        |        0 |
| 0     |   0        |        0 |
|-------+------------+----------|

Then I created the following Series ( D ) in pandas: 然后,我在熊猫中创建了以下系列( D ):

D = pd.Series((sht[sht!=0]).values.flatten()).dropna().reset_index(drop=True)
D

|----------|
| jon      |
| tyrion   |
| daenerys |
| sansa    |
| cersei   |
| rhaegar  |
| arya     |
| jaime    |
| bran     |
| tywin    |
| rob      |
|----------|

How could I insert the Series D in the column D of sht (the "Table" sheet of my spreadsheet)? 如何将D系列插入sht的D列(电子表格的“表格”表)?

I tried: 我试过了:

writer = pd.ExcelWriter(path, engine='openpyxl')
K.to_excel(writer,'Table', startrow=0, startcol=4, header=False, index=False)
writer.save()

But it deletes all the other tabs from my spreadsheet and also erases the values in the A:C columns of my spreadsheet... 但这会从电子表格中删除所有其他标签,并且还会删除电子表格的A:C列中的值...

The pd.ExcelWriter and .to_excel method in pandas overwrite the existing file. 大熊猫中的pd.ExcelWriter.to_excel方法将覆盖现有文件。 You are not modifying the existing file, but are deleting it and writing a new file with the same name. 您不是要修改现有文件,而是要删除它并写入具有相同名称的新文件。

If you want to write to an existing excel file, you probably want to use openpyxl . 如果要写入现有的excel文件,则可能要使用openpyxl

import openpyxl

# open the existing file
wb = openpyxl.load_workbook('myfile.xlsx')

# grab the worksheet.  my file has 2 sheets: A-sheet and B-sheet
ws = wb['A-sheet']

# write the series, D, to the 4th column 
for row, v in enumerate(D, 1):
    ws.cell(row, 4, v)

# save the changes to the workbook
wb.save('myfile.xlsx')

Try this 尝试这个

  • Open excel file and store contents in panda object lets say df. 打开excel文件并将内容存储在panda对象中,例如df。
  • Create the new column in df['new column'] 在df ['new column']中创建新列
  • Store the value in the new column,now panda has all ur previous values + the new column 将值存储在新列中,现在熊猫拥有您所有的先前值+新列
  • Save the df to excel file using ExcelWriter 使用ExcelWriter将df保存到excel文件

PS:while opening excel file and writing excel file in pandas there is option to open a specific sheet ie sheet_name= PS:打开excel文件并在熊猫中写入excel文件时,可以选择打开特定的工作表,即sheet_name=

暂无
暂无

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

相关问题 将 pandas 系列或 DataFrame 列插入现有 Excel 文件的第一个空列(使用 OpenPyXL?) - Insert pandas Series or DataFrame column into existing Excel file's first empty column (with OpenPyXL?) 使用Pandas Python将系列插入CSV文件的特定列 - Insert a series into specific column of a CSV file using Pandas Python 如何在不使用pandas更改文件中的现有数据的情况下将新列附加到Excel文件? - How to append a new column to an Excel file without changing the existing data on the file using pandas? 如何将系列写入 csv 文件中的特定现有列? - How do I write series to specific existing column in csv file? 如何在 Python 中现有的 excel 文件中向特定单元格插入值? - How to insert a value to a specific cell in an existing excel file in Python? 如何在不覆盖数据的情况下写入现有的 excel 文件(使用熊猫)? - How to write to an existing excel file without overwriting data (using pandas)? 使用pandas从excel文件中读取特定列 - Reading specific column from excel file using pandas 如何在 pandas 的 csv 文件的特定列的行范围内插入文本? - How to insert text in a range of row in a specific column in a csv file in pandas? 如何在不删除以前的数据的情况下写入现有的excel 10000次文件? - How to write to an existing excel 10000 times file without deleting the previous data? 如何使用 pandas 写入现有 excel 文件而不覆盖现有数据 - How to write to an existing excel file without over-writing existing data using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM