如何从工作簿B中调用Workbook_Open on Workbook A?(How can I call Workbook_Open on Workbook A from Workbook B?)
我的情况是我试图从工作簿B打开工作簿A并运行Workbook_Open子例程。 仅仅使用
Workbooks.Open("c:\myworkbook.xls")
是我需要在Workbook_Open
子例程运行之前在Workbook A中Workbooks.Open("c:\myworkbook.xls")
数据。我有两个想法,我都不喜欢,所以我想知道是否有更好的方法。 我的两个想法如下。
选项1:打开工作簿A,禁用宏,植入数据,保存并关闭工作簿,然后在启用宏的情况下重新打开它。 我不想这样做的原因是:我正在处理一个具有相当数量格式的.xls文件(基本上我模拟人类将如何使用它数千次)并保存文件数千次可能导致文件损坏。 此外,这需要相当长的时间才能打开工作簿两次,而且对我来说似乎没有任何效率。 其中一个重点是速度和效率,以及最快的周转时间。
选项2:将Workbook_Open代码复制为模块中其他位置的公共子例程。 这是两者中更为理想的问题,但问题是我不一定有权执行此操作,这样做会涉及大量繁文缛节,红旗等。
有没有办法做这样的事情:
Workbooks("Workbook A").Application.Run (Workbooks("Workbook A").Workbook_Open)
My situation is that I am trying to open Workbook A from Workbook B and run the Workbook_Open subroutine. The issue with just simply using
Workbooks.Open("c:\myworkbook.xls")
is that I need to plant data in Workbook A before theWorkbook_Open
subroutine runs.I have two ideas, neither of which I particularly like, so I was wondering if there was a better way. My two ideas are as follows.
Option 1: Open Workbook A with macros disabled, plant the data, save and close the workbook then re-open it with macros enabled. The reasons I would prefer not to do this is: I am dealing with a .xls file that has a fair amount of formatting (basically I am simulating how a human would use it thousands of times over) and saving the file thousands of times may cause the file to corrupt. Additionally this takes a fair amount of time to open the workbook twice, and it does not seem at all efficient to me. One of the big points of emphasis is speed and efficiency with the quickest turnaround time.
Option 2: Duplicate the Workbook_Open code as a public subroutine in elsewhere within a module. This is the more desirable of the two, but the issue is I do not necessarily have permission to perform this action and doing this will involve plenty of red tape, red flags, etc.
Is there any way to do something like this:
Workbooks("Workbook A").Application.Run (Workbooks("Workbook A").Workbook_Open)
原文:https://stackoverflow.com/questions/27869616
最满意答案
将
DataFrame
视为Series
的集合会很有用,其中每个Series
都是DataFrame
一列。当您沿
axis=1
取平均值时,您将获得一个Series
其中包含Panel
10个项目中每个项目的30个元素。 如果您收集这10个Series
您将获得一个包含30行和10列的DataFrame
。类似地,当你沿着
axis=2
取平均值时,你会得到一个长度为20的Series
,用于面板中的每个项目。 将这10个Series
收集在一起,可以得到一个包含20行和10列的DataFrame
。当您计算沿
axis=0
的平均值axis=0
,您将获得平均项目,该项目预期与面板中的每个项目具有相同的形状,即20乘30。下面的一些代码可能有助于阐明我的观点:
In [1]: panel = pd.Panel(np.random.rand(10,20,30)) # Note that for a single item in the panel, the mean along # either of its axes is a `Series`: In [2]: type(panel[0].mean(axis=0)) Out[2]: pandas.core.series.Series In [3]: type(panel[0].mean(axis=0)) Out[3]: pandas.core.series.Series In [3]: panel[0].mean(axis=0) Out[3]: 0 0.569390 1 0.497762 2 0.616333 3 0.608153 4 0.524008 5 0.478621 6 0.492827 7 0.461240 8 0.573557 9 0.654757 10 0.541066 11 0.572929 12 0.482214 13 0.576898 14 0.362965 15 0.493355 16 0.491790 17 0.567756 18 0.426404 19 0.514235 20 0.416983 21 0.455843 22 0.603592 23 0.387520 24 0.470060 25 0.506414 26 0.545156 27 0.525211 28 0.526378 29 0.486419 dtype: float64
It can be useful to think of a
DataFrame
as a collection ofSeries
, where eachSeries
is a column in theDataFrame
.When you take the mean along
axis=1
, you obtain aSeries
with 30 elements for each of the 10 items in thePanel
. If you collect these 10Series
you obtain aDataFrame
with 30 rows and 10 columns.Similarly , when you take the mean along
axis=2
, you end up with aSeries
of length 20 for each of the 10 items in the panel. Collecting these 10Series
together gives you aDataFrame
with 20 rows and 10 columns.When you calculate the mean along
axis=0
, you obtain the average item, which is expected to have the same shape as each item in the panel, i.e. 20 by 30.Below some code that may help elucidate my point:
In [1]: panel = pd.Panel(np.random.rand(10,20,30)) # Note that for a single item in the panel, the mean along # either of its axes is a `Series`: In [2]: type(panel[0].mean(axis=0)) Out[2]: pandas.core.series.Series In [3]: type(panel[0].mean(axis=0)) Out[3]: pandas.core.series.Series In [3]: panel[0].mean(axis=0) Out[3]: 0 0.569390 1 0.497762 2 0.616333 3 0.608153 4 0.524008 5 0.478621 6 0.492827 7 0.461240 8 0.573557 9 0.654757 10 0.541066 11 0.572929 12 0.482214 13 0.576898 14 0.362965 15 0.493355 16 0.491790 17 0.567756 18 0.426404 19 0.514235 20 0.416983 21 0.455843 22 0.603592 23 0.387520 24 0.470060 25 0.506414 26 0.545156 27 0.525211 28 0.526378 29 0.486419 dtype: float64
相关问答
更多-
这是一个在pandas 0.13.1中修复的错误(尽管不在0.13.0中)。 更新可解决此问题。 请参阅Jeff对该问题的评论以及github错误报告 This is a bug which was fixed in pandas 0.13.1 (though not in 0.13.0). Updating resolves the issue. See Jeff's comments to the question as well as the github bug report
-
在熊猫中交换轴(Swapping Axes in Pandas)[2023-06-26]
转置时获得战利品 In [4]: df1.T Out[4]: 0 1 2 3 one 1 2 3 4 two 4 3 2 1 Take a loot at transpose In [4]: df1.T Out[4]: 0 1 2 3 one 1 2 3 4 two 4 3 2 1 -
熊猫面板合并(Pandas Panel Merge)[2023-09-11]
我将转换为combine_first , combine_first ,然后再转回 new = p.to_frame().combine_first(dp.to_frame()).to_panel() print new[:,:,'Close'] AA IBM Z major 2000-01-01 1.348884 0.472272 NaN 2000-01-02 ... -
选项1 pd.concat pd.concat({i: d.set_index('context').time for i, d in pn.iteritems()}).unstack() context foo bar baz 0 21 37 53 1 36 42 76 2 24 56 83 3 17 32 45 选项2 pd.DataFrame pd.DataFrame([d.set_index(' ...
-
pn = pd.Panel(np.arange(27).reshape(3, 3, 3), list('abc'), list('xyz'), list('ijk')) 在pd.Panel apply默认情况下迭代2-D横截面。 这意味着它的名称必须从其他维度引用索引。 例如 pn.apply(lambda s: type(s)) ('a', 'i') ('a', 'j') ('a', 'k') ('b', 'i') ('b', 'j') ('b', 'k') ('c', 'i') ('c', 'j') ...
-
在我看来, panel已过时 - 查看文档 ,所以如果想要使用pandas一个可能的解决方案是MultiIndex DataFrame ,如果可能的话,最好的方法是在DataFrame为标量值转换字典以获得更好的性能并使用熊猫函数。 然后检查: print (df.columns.names) 和 print (df.index.names) 但是如果要使用panel : wp = pd.Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'], ...
-
使用to_frame()方法。 要获得所需的输出,您需要转置轴并在之后重置索引。 In [31]: p.transpose(2, 0, 1).to_frame().reset_index(drop=True) Out[31]: 0 1 2 3 0 0.780538 0.412587 0.587926 0.217103 1 0.019309 0.246170 0.965017 0.839562 2 0.742044 ...
-
In [27]: for key in pnl.minor_axis : ....: print key ....: print pnl.minor_xs (key) In [27]: for key in pnl.minor_axis : ....: print key ....: print pnl.minor_xs (key)
-
将DataFrame视为Series的集合会很有用,其中每个Series都是DataFrame一列。 当您沿axis=1取平均值时,您将获得一个Series其中包含Panel 10个项目中每个项目的30个元素。 如果您收集这10个Series您将获得一个包含30行和10列的DataFrame 。 类似地,当你沿着axis=2取平均值时,你会得到一个长度为20的Series ,用于面板中的每个项目。 将这10个Series收集在一起,可以得到一个包含20行和10列的DataFrame 。 当您计算沿axis= ...
-
看起来(如果zorder与你的例子相反)那么你的第一个(较大的)轴的“不透明”白色背景与第二个(较小的)轴重叠,所以一种方法是简单地设置较大的轴的颜色轴到透明。 另外一定要将整个图形的面颜色设置为白色...... f.set_facecolor('white') ax2 = f.add_axes([0.3,0.2,0.5,0.4], zorder=1) ax2.patch.set_facecolor('gray') ax2.plot([0,1], [0,3], color='red') ax1 = f. ...