我拼命地尝试使用Pyxll编写一些excel函数来获取一堆数组,在Python中加载它们,将它们转换为pandas DataFrames,稍微播放数据然后返回最终的DataFrame 。 现在,为了返回我找到pyxll examples的DataFrame,但无论我如何尝试,我似乎无法将我加载的excel数组转换为我可以使用的pandas DataFrame。
例如,我尝试使用下面的代码,但没有运气。也许如果我有某种方式知道在Python中加载什么以及它看起来如何,也许我有更好的机会去了解如何操纵数据,但我不知道如何在我的Canopy上查看输出输出区域。
有没有人知道从excel导入数据到python的简单方法,处理它然后将其返回到excel,而不必保存文件,在python中加载它,处理数据并覆盖现有文件?
@xl_func("string[] name, var[] day, string[] method, string[] currency, numpy_array amounts, date[] dates: dataframe")
def test(name, day, method, currency, amounts, dates):
df_name = DataFrame(name, columns = ['Name'])
df_method = DataFrame(method, columns = ['Method']).ix[1:]
df_currency = DataFrame(currency, columns = ['Currency']).ix[1:]
df = df_name.join(df_method).join(df_currency)
cols = ['Name', 'Currency', 'Method']
df = df[cols]
return DataFrame(dates)
答案 0 :(得分:4)
查看(我的)库xlwings。它使得发送DataFrames变得如此简单:
>>> from xlwings import Workbook, Range
>>> import pandas as pd
>>> wb = Workbook() # Pass in the path of a file to work with an existing Workbook
>>> df = pd.DataFrame([[1., 2.], [3., 4.]], columns=['one', 'two'])
>>> Range('A1', index=False).value = df # send it over to Excel
>>> data = Range('A1').table.value # read it back
>>> pd.DataFrame(data[1:], columns=data[0])
one two
0 1 2
1 3 4
请参阅有关DataFrames的文档以及如何从VBA调用它。
答案 1 :(得分:4)
PyXLL可以使用自定义类型接受和返回pandas数据帧。
看看这个例子: https://github.com/pyxll/pyxll-examples/blob/master/pandas
要查看输出,请查看日志文件。 要使用IPython提示在Python中以交互方式进行游戏,请使用以下示例: https://github.com/pyxll/pyxll-examples/tree/master/ipython
更好地坚持使用功能而不是诉诸命名范围。
您也可以使用PyXLL注册键盘快捷键。有关自动调整数组公式输出大小的快捷方式,请参阅此示例: https://github.com/pyxll/pyxll-examples/blob/master/shortcuts/resize_array_formula.py
如果您需要更多帮助,请联系支持部门以获得快速回复。
答案 2 :(得分:2)
查看xlrd
(Python 2和3),xlwt
(仅限Python 2)和xlsxwriter
(Python 2和3)模块,以便与pandas
一起使用。熊猫在它的代码中有它们的钩子;您可以阅读有关read_excel
和to_excel
函数here的所有内容。