DataNitro: 用Python高效处理Excel数据
来源:互联网 发布:做奥数题的软件 编辑:程序博客网 时间:2024/05/15 03:56
Contents
Installation
Excel Interface
Hello World
Calculating Pi
Sample Scripts
Installation
Please close Excel and run the installer to get started.
Excel Interface
When you start Excel, you'll have a new DataNitro tab:
- Editor
- Launches a custom Idle editor. You can use it, or any other editor, to edit Python code. Once you're done, save it as a .py file and run it.
- Python Shell
- Launches a python shell you can use to interact with the spreadsheet directly.
- import
- Adds a script to the imported script list.
- remove
- Removes the script from the list.
- Run
- Runs the selected script.
- Stop
- Stops a running script.
- Docs
- This links to the documentation.
- Add-ins
- This links to our contact information.
- Settings
- Modify DataNitro's settings.
Hello World
Hello world with DataNitro is short:
Cell("A1").value = "Hello, World!"That's it!
Calculating Pi
Let's approximate pi with a Monte Carlo simulation. We'll generate a random 2D point with x and y coordinates between 0 and 1, and see how far it is from the origin, specifically, if it's inside the unit circle. The total area of the points we're choosing from is 1, and the area of the points inside the circle and in our box is pi/4, the fraction of points that fall within our radius should be about pi/4.
We'll use Excel to keep track of our trials, and the variables within each trial. First, let's import random, and label a few cells so we know what's what:
from random import randomCell("A2").value, Cell("A3").value, Cell("A4").value = "Trial:","Hits:","pi is about:"Cell("D1").value, Cell("E1").value, Cell("F1").value = "x","y","x^2 + y^2"Next, we should decide on the number of trials. Let's allow the user to set a number by entering it in the first cell, and use 100 as a fallback.
trials = Cell("A1").valueif (type(trials)!= int) or (trials < 1): trials = 100Now, we'll run the trials. We'll make a function for this, and keep track of the coordinates of our random points, as well as their distance from the origin, under the headings we made. We'll also keep track of the number of trials.
def pi_calc(n): while Cell("B2").value < n: x, y = Cell("D2"), Cell("E2") r = Cell("F2") x.value, y.value = random(), random() r.value = x.value**2 + y.value**2We need to increment the number of trials each time, and also increment "Hits" if our point falls within the circle.
Cell("B2").value += 1 if r.value <= 1: Cell("B3").value += 1We can now approximate pi as 4 times the fraction of hits to trials:
Cell("B4").value = 4*float(Cell("B3").value)/Cell("B2").valueLet's add a small delay at the end of each run so we can watch the function update.
from time import sleep...def pi_calc(n): ... sleep(0.01)Finally, let's run our function, making sure to reset the number of trials and number of hits beforehand:
Cell("B2").value, Cell("B3").value = 0,0pi_calc(trials)
We're done! Our finished script:
# Generates approximations of pi. Put the number of trials you want in A1. More# trials will result in higher accuracy.from random import randomfrom time import sleepdef pi_calc(n): while Cell("B2").value < n: x, y = Cell("D2"), Cell("E2") r = Cell("F2") x.value, y.value = random(), random() r.value = x.value**2 + y.value**2 Cell("B2").value += 1 if r.value <= 1: Cell("B3").value += 1 Cell("B4").value = 4*float(Cell("B3").value)/Cell("B2").value sleep(0.01)trials = Cell("A1").valueif (type(trials)!= int) or (trials < 1): trials = 100# sheet setupCell("A2").value, Cell("A3").value, Cell("A4").value = "Trial:","Hits:","pi is about:"Cell("D1").value, Cell("E1").value, Cell("F1").value = "x","y","x^2 + y^2"Cell("B2").value, Cell("B3").value = 0,0pi_calc(trials)More trials will give better accuracy. 1000 should be enough to approximate pi to two digits.
Sample Scripts
DataNitro comes with two sample scripts in the example folder: hello_world.py, a hello world program, and pascal_triangle.py, which prints out Pascal's triangle. You can also download and play withpi.py, a spreadsheet and script for binomial option pricing, and the ystockquote script for pulling data from yahoo finance.
If you have any questions, contact us!
You can find more detailed documentation.
from: https://datanitro.com/trial
- DataNitro: 用Python高效处理Excel数据
- datanitro:python程序员可以对 excel VBA 说不
- Python处理Excel数据
- Python处理Excel数据
- Python处理excel数据
- Python 处理Excel数据
- 利用Python处理Excel数据
- VS2008下用C++ OLE技术高效处理Excel数据-总结
- Excel VBA高效办公应用-第四章-用VBA处理市场调查问卷数据
- 用Python如何处理excel里面的数据
- Python处理Excel(三):内置数据类型处理Excel数据
- Python处理Excel(四):自定义类处理Excel数据
- oracle 高效处理数据
- 用npoi处理excel数据
- 大数据EXCEL高效导出
- 【Python】Python处理Excel
- python结合Arcpy处理EXCEL数据生成多边形
- python数据分析-处理CSV/EXCEL表格文件
- Android中SharedPreferences的使用
- JavaWeb操作Msql数据库显示数据流程(源代码+详细步骤)
- Static关键字
- 八大排序之选择排序
- Python多线程中的join函数的使用与含义
- DataNitro: 用Python高效处理Excel数据
- java中的网络通信
- storyboard 修改 无反应
- 插入排序
- BZOJ4551 [Tjoi2016&Heoi2016]树
- POJ2484
- 利用Python实现简单的相似图片搜索的教程
- 用java实现一个简单的ArrayList
- 详解Gson使用(四)Map对象转化