Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python编写excel工作表_Python_Excel_Pandas - Fatal编程技术网

用python编写excel工作表

用python编写excel工作表,python,excel,pandas,Python,Excel,Pandas,我试着用panda编写excel表格,我成功地做到了。 我必须回答以下问题: 首先我尝试按列排序,但无法。我使用了df.sort_索引,df.sortlevel 两人都没有得到我想要的。 这是我的代码: df = DataFrame({'Seq Label':['if >= 1','if >= 2','if >= 3','if >= 4','if >= 5','if >= 6','if >= 7'], 'TP':[countTP,

我试着用panda编写excel表格,我成功地做到了。 我必须回答以下问题: 首先我尝试按列排序,但无法。我使用了df.sort_索引,df.sortlevel 两人都没有得到我想要的。 这是我的代码:

df = DataFrame({'Seq Label':['if >= 1','if >= 2','if >= 3','if >= 4','if >= 5','if >= 6','if >= 7'],
            'TP':[countTP, twocountTP, threecountTP, fourcountTP, fivecountTP, sixcountTP, sevencountTP], 
            'TN':[countTN, twocountTN, threecountTN, fourcountTN, fivecountTN, sixcountTN, sevencountTN], 
            'FP':[countFP, twocountFP, threecountFP, fourcountFP, fivecountFP, sixcountFP, sevencountFP], 
            'FN':[countFN, twocountFN, threecountFN, fourcountFN, fivecountFN, sixcountFN, sevencountFN]})
df.to_excel('Book10.xlsx', sheet_name='sheet1', index=False)
它提供了我不想要的输出:

    FN   FP Seq Label     TN    TP
0  123  125   if >= 1  20296  7671
1  123  125   if >= 2  17142  6274
2  123  125   if >= 3   3810  1307
3    7   11   if >= 4    419   213
4    1    4   if >= 5    127    74
5    0    0   if >= 6      0     0
6    0    0   if >= 7      0     0
我想它排序表的顺序,我在我的df中的代码。我想要它 将其排序为:

Seq Label   TP   TN    FP    FN
第二个问题 如何在现有excel工作表上写入而不删除或写入其他数据。 我尝试过使用不同的LIB。比如

import pandas as pd
import openpyxl
import xlrd
如果我把时间拖得太长,我很抱歉。我需要你的帮助
谢谢

首先对数据框进行排序,然后将其写入Excel

df.sort_values(['Seq Label', 'TP', 'TN', 'FP', 'FN']).to_excel(...)
编辑

哦,您只想将列重新排列为所需的顺序。尝试:

df[['Seq Label', 'TP', 'TN', 'FP', 'FN']].to_excel(...)
字典中项目的顺序不保证。如果您使用字典来构建数据帧,并考虑目标顺序,则可以执行以下操作:

desired_order = [['Seq Label', 'TP', 'TN', 'FP', 'FN']]
df_order = [k for k in df if k in desired_order] + [k for k in df if k not in desired_order]
df = df[df_order]

如果键存在于数据帧中,则这将按所需顺序对数据帧进行排序。任何不符合所需顺序的列都将被追加到列的右侧。

根据需要在数据框
df
中重新排列列,然后写入excel。这样,您就不必担心excel end。
注意:列中的值是虚构的

>>> df = df[['Seq Label', 'TP', 'TN', 'FP', 'FN']]
>>> df
  Seq Label  TP  TN  FP  FN
0   if >= 1   1   1   1   1
1   if >= 2   2   2   2   2
2   if >= 3   3   3   3   3
3   if >= 4   4   4   4   4
4   if >= 5   5   5   5   5
5   if >= 6   6   6   6   6
6   if >= 7   7   7   7   7
>>> df.to_excel('Book10.xlsx', sheet_name='sheet1', index=False)
结果

在这里,您可以给出行和列的编号,或者使用for循环来写入连续的数据单元。我用过这个。这对我来说是完美的工作


请每个帖子只问一个问题。我没法回答。正如你在我的数据框中所看到的,我使用了{}~dict。我不能按我的顺序得到它。你说的“不工作”是什么意思。错误是什么?它给出了我发布的第一个表,这是我不想要的。我需要在数据框中输入的相同顺序。我需要第一列为“序列标签”,第二列为“TP”",... etcif我的答案解决了您的问题请单击答案旁边的向上箭头接受答案,以便社区知道问题已解决。关于第二个问题,你有什么想法?我感谢你的帮助我确实点击了向上箭头,但不允许,因为我的名声不到15岁。谢谢您的想法是的,您需要获取现有工作表的最后一行,并指定下一行用于写入数据。它变得有点复杂。每次问一个问题是明智的,否则会造成混乱。如果你问另一个问题,让我知道,我可以详细回答。或搜索此关键字“从python到excel追加”
from xlutils.copy import copy  # http://pypi.python.org/pypi/xlutils
from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd
from xlwt import easyxf # http://pypi.python.org/pypi/xlwt

rb = open_workbook('file.xlrd',formatting_info=True)
r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file
wb = copy(rb) # a writable copy (I can't read values out of this, only write to it)
w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy
w_sheet.write(row, col, 'Value') 
wb.save(file_path)