You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To write to other cells, you would need to manually enter or do it via automation (i.e. an Office Script to write out the =PY formula) to the desired cell.
I regularly build physics models on excel that calculate a certain output value based on given parameters. Then I always want to plot the relationship that a chosen input parameter has to the output. If I worked only in Python, it's easy: I would make a function and iterate (loop) to plot. Now, I want to achieve the same but using python in excel.
See the example below. Here I want to plot input parameter (C2) against output (C35). I achieved the desired output in "sheet=DP-Python", but this is not elegant enough, I would then rather just do it all in python.
I tried using the fact that python in excel goes left to right and then down, but then I get a circular ref issue.
If there is an elegant way to do this, it would work well for monte carlo simulations as well.
I know I can do this using a macro, but again then I'll just rather do it all in Python.
I would think that you can dump several output variables as a dataframe on your sheet. From there you could possibly reference to other cells on your sheet(s).
If you want, we could discuss the details in private.
Activity
keyur32 commentedon Sep 20, 2023
Python formulas only return data within the cell they were entered on. https://support.microsoft.com/en-us/office/data-security-and-python-in-excel-33cc88a4-4a87-485e-9ff9-f35958278327.
To write to other cells, you would need to manually enter or do it via automation (i.e. an Office Script to write out the =PY formula) to the desired cell.
jflam commentedon Sep 25, 2023
You can also spill additional values back into the grid as well if you return lists, tuples or Pandas DataFrames from the Python formula.
WillemWannenburg commentedon Oct 23, 2023
ahagmann83, have you found a work-around way to write from Python to Excel?
jflam commentedon Oct 23, 2023
There are no workarounds for this, as this behavior is by design. What is your scenario?
WillemWannenburg commentedon Oct 24, 2023
I regularly build physics models on excel that calculate a certain output value based on given parameters. Then I always want to plot the relationship that a chosen input parameter has to the output. If I worked only in Python, it's easy: I would make a function and iterate (loop) to plot. Now, I want to achieve the same but using python in excel.
See the example below. Here I want to plot input parameter (C2) against output (C35). I achieved the desired output in "sheet=DP-Python", but this is not elegant enough, I would then rather just do it all in python.
I tried using the fact that python in excel goes left to right and then down, but then I get a circular ref issue.
Pressure analysis - python for excel.xlsx
If there is an elegant way to do this, it would work well for monte carlo simulations as well.
I know I can do this using a macro, but again then I'll just rather do it all in Python.
salabim commentedon Oct 24, 2023
I would think that you can dump several output variables as a dataframe on your sheet. From there you could possibly reference to other cells on your sheet(s).
If you want, we could discuss the details in private.
WillemWannenburg commentedon Oct 25, 2023
In summary what I would want to do in a python cell in excel:
WillemWannenburg commentedon Oct 30, 2023
I found out I can achieve my goal using a What-if -> Data Table. https://www.reddit.com/r/excel/comments/5sdlng/whatif_analysis_question/