添加公式后,使用pandas / xslxwriter对列进行排序

皮奥特·科诺普尼克(Piotr Konopnicki)

我的数据框只有很少的列,实际上与这个问题无关,但是我想按特定顺序对列进行排序。

现在,问题是我有很多引用excel表的公式(我正在使用xslxwriter worksheet.add_table创建),例如:

planned_units = '=Table1[@[Spend]]/Table1[@[CP]]'

因此,如果我通过在熊猫中添加一列来添加这些公式:

df['newformula'] = planned_units

我认为这是行不通的,因为我在实际添加表格之前添加了引用表格的公式。因此,在添加公式之前对这些列进行排序将不起作用,因为:

  • 稍后(在创建表之后)添加公式,但是我还想对刚添加的列进行排序
  • 如果我要在add_table之前添加引用excel表的公式,则这些公式在excel中将不起作用

似乎xslxwriter不允许我以任何方式对列进行排序(也许是我错了吗?),所以我没有最终的“产品”(在将所有列都添加了公式之后)对列进行排序的可能性。

最好使用工作公式而不是排序的列,但是我很乐意欢迎任何有关如何对它们进行排序的想法。

谢谢!

PS代码示例:

import pandas as pd
import xlsxwriter


# simple dataframe with 3 columns

input_df = pd.DataFrame({'column_a': ['x', 'y', 'z'],
                         'column_b': ['red', 'white', 'blue'],
                         'column_c': ['a', 'e', 'i'],
                         })

output_file = 'output.xlsx'

# formula I want to add
column_concatenation = '=CONCATENATE(Table1[@[column_a]], " ", Table1[@[column_b]])'

# now if adding formulas with pandas would be possible, I would do it like this:

# input_df['concatenation'] = column_concatenation
# but its not possible since excel gives you errors while opening!

# adding excel table with xlsxwriter:

workbook = xlsxwriter.Workbook(output_file)
worksheet = workbook.add_worksheet("Sheet with formula")

# here I would change column order only IF formulas added with pandas would work! so no-no
'''
desired_column_order = ['columnB', 'concatenation', 'columnC', 'columnA']
input_df = input_df[desired_column_order]
'''
data = input_df

worksheet.add_table('A1:D4', {'data': data.values.tolist(),
                              'columns': [{'header': c} for c in data.columns.tolist()] +
                                         [{'header': 'concatenation',
                                           'formula': column_concatenation}
                                          ],
                              'style': 'Table Style Medium 9'})

workbook.close()

现在在workbook.close()之前,我很乐意使用该表'desired_column_order'在添加公式后对列进行重新排序。

谢谢:)

杰姆纳马拉

看起来这里有两个问题:排序和表公式。

排序是Excel在运行时在Excel应用程序中执行的操作,不是文件格式的属性或可以触发的格式。由于XlsxWriter仅处理文件格式,因此无法进行任何排序。但是,在使用XlsxWriter编写数据之前,可以在Python / Pandas中对数据进行排序。

出现公式问题是由于Excel具有原始[#This Row]语法(Excel 2007)和更高版本的@语法(Excel 2010+)。请参阅有关使用工作表表-列的XlsxWriter文档

公式中支持Excel 2007样式[#This Row]和Excel 2010样式@结构引用。但是,不支持其他Excel 2010对结构引用的添加,并且公式应符合Excel 2007样式公式。

因此,基本上,您需要使用Excel 2007语法,因为这是文件格式存储的内容,即使Excel在外部显示Excel 2010+语法也是如此。

当您通过add_table()方法XlsxWriter添加公式时,会为您进行转换,但是如果您以其他方式(例如通过Pandas)添加公式,则需要使用Excel 2007语法。因此,而不是像这样的公式:

=CONCATENATE(Table1[@[column_a]], " ", Table1[@[column_b]])

您需要添加以下内容:

=CONCATENATE(Table1[[#This Row],[column_a]], " ", Table1[[#This Row],[column_b]])

(您可以看到为什么在更高的Excel版本中将其转换为较短的语法的原因。)

然后您的程序将按预期工作:

import pandas as pd
import xlsxwriter

input_df = pd.DataFrame({'column_a': ['x', 'y', 'z'],
                         'column_b': ['red', 'white', 'blue'],
                         'column_c': ['a', 'e', 'i'],
                         })

output_file = 'output.xlsx'

column_concatenation = '=CONCATENATE(Table1[[#This Row],[column_a]], " ", Table1[[#This Row],[column_b]])'

input_df['concatenation'] = column_concatenation


workbook = xlsxwriter.Workbook(output_file)
worksheet = workbook.add_worksheet("Sheet with formula")

desired_column_order = ['column_b', 'concatenation', 'column_c', 'column_a']
input_df = input_df[desired_column_order]
data = input_df

# Make the columns wider for clarity.
worksheet.set_column(0, 3, 16)

worksheet.add_table('A1:D4', {'data': data.values.tolist(),
                              'columns': [{'header': c} for c in data.columns.tolist()] +
                                         [{'header': 'concatenation'}],
                              'style': 'Table Style Medium 9'})

workbook.close()

输出:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章