简体   繁体   English

使用数据框将列表写入新的Excel xlsx

[英]Writing a list to new excel xlsx with dataframes

I am having some trouble finding the best way to write a list to a loaded excel sheet and then saving the result as an xlsx. 我在寻找将列表写入已加载的Excel工作表的最佳方法,然后将结果另存为xlsx时遇到了一些麻烦。 What I want my code to do is to take the lists that I have created and put them in specific columns of the loaded workbook. 我要我的代码执行的操作是获取我创建的列表,并将它们放在已加载的工作簿的特定列中。 I think there is probably an easier way to do this using dataframes, but I do not know how. 我认为使用数据框可能有更简单的方法,但是我不知道如何做。 Ideally, I would like to save the formatting of the loaded workbook. 理想情况下,我想保存加载的工作簿的格式。

col_test = [1L, 2L, 3L, 4L, 5L]

My code is this 我的代码是这样

import xlrt
from xlrd import open_workbook
rb = open_workbook("Excel FDT Master_01_update.xlsx")
s = rb.sheet_by_name('INPUT')
r = 5
for test in col_test:
    s.cell(row = r, column = 1).value = test
    r += 1
rb.save('didthiswork.xlsx')

That error means that Python could not find a parameter named row in the definition of cell . 该错误意味着Python在cell的定义中找不到名为row的参数。 If I'm not mistaken this is the xlrd module. 如果我没记错的话,这是xlrd模块。 Let's take a look at the API documentation for Sheet.cell() which you are attempting to call here. 让我们看一下您试图在此处调用的Sheet.cell()的API文档。

cell(rowx, colx)

Cell object in the given row and column. 给定行和列中的单元格对象。

It appears that you've simply misnamed the parameters. 看来您只是错误地命名了参数。 Changing the line to the following should fix this. 将行更改为以下内容可以解决此问题。

s.cell(rowx = r, colx = 1).value = test

It goes without saying that Python cannot make guesses as to what you meant to type, so whenever you get an error about something not existing when you're sure it does, like the parameter names here, make sure to read the documentation and check for typos. 毋庸置疑,Python无法猜测您要键入的内容,因此,如果在确定确实存在错误时(例如此处的参数名称)遇到错误,请务必阅读文档并检查是否存在错误错别字。 Also, in the future post all relevant info you can find, such as the function definition and the names of the modules you are using. 另外,将来发布所有您可以找到的相关信息,例如功能定义和所使用模块的名称。

Here is one version with no extra installs on top of anaconda. 这是一个版本,在anaconda之上无需额外安装。 It is not keeping styling, but that you can fix with a copy/'paste values' back to original xlsx. 它并没有保持样式,但是您可以将复制/粘贴值恢复为原始xlsx。

Most excel manipulators have issues with keeping the original file intact. 大多数excel操纵器在保持原始文件完整方面存在问题。 There are ways around that as well, but if you want it to be waterproof, you basically end up with a specific solution for you, or more or less recode all libraries out there, so it's not worth the effort. 也有很多解决方法,但是如果您希望它防水,则最终会找到适合您的特定解决方案,或者或多或少地重新编码所有库,因此这是不值得的。

Pandas can be a bit tricky to get right when extending existing dataframes, but there are always several alternative ways to do it. 扩展现有数据帧时,熊猫可能很难正确设置,但是总有几种替代方法可以做到。 Here it's done with assign, so then one only needs to make sure that the dataframe's rowcount is long enough for what one wants to add. 这是通过assign完成的,因此只需确保数据框的行数足够长即可添加。

import pandas as pd

# read the excel
df = pd.read_excel('Geen titel 1.xlsx') # there are options to choose sheet
print('original df')
print(df)
# your data is not valid python syntax so let's assume it's strings
col_test = ['1L', '2L', '3L', '4L', '5L']
new_idx = range(max(len(col_test), len(df.index)))
df = df.reindex(new_idx) # now it will accommodate different lengths
print('reindexed df')
print(df)
df = df.assign(new_col=col_test) # new column added at right side
print('modified df')
print(df)
df.to_excel('the_new.xlsx')

The printouts: 打印输出:

original df
   a  b
0  1  c
1  2  d
2  3  e
reindexed df
     a    b
0  1.0    c
1  2.0    d
2  3.0    e
3  NaN  NaN
4  NaN  NaN
modified df
     a    b new_col
0  1.0    c      1L
1  2.0    d      2L
2  3.0    e      3L
3  NaN  NaN      4L
4  NaN  NaN      5L

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

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