2012-10-22 85 views
1

這可能很簡單,但在查看過去幾個小時的文檔和可能的示例之後,我無法弄清楚。從Stata調用Python

我有一個很大的數據集(電子表格),通過DO文件得到大量清理。然後,在DO文件中,我想將清理過的數據的某些變量保存爲temp .csv,然後運行一些Python腳本,這些腳本會產生一個新的CSV,然後將該輸出附加到我清理過的數據中。

如果不清楚這裏是一個例子。

After cleaning my data set (XYZ) goes from variables A to Z with 100 observations. I want to take variables A and D through F and save it as test.csv. I then want to run a python script that takes this data and creates new variables AA to GG. I want to then take that information and append it to the XYZ dataset (making the dataset now go from A to GG with 100 observations) and then be able to run a second part of my DO file for analysis.

我一直在做手工這一點,它是好的,但該文件將開始迅速變化,它會爲我節省大量的時間。

回答

0

在Stata中輸入「help shell」。你想要做的就是從Stata中抽出時間,調用Python,然後讓Stata在Python腳本完成之後繼續任何你想要的操作。

1

將這項工作(假設你可以得到蟒蛇

tempfile myfiletemp 
save `myfiletemp' 
outsheet myfile1.csv 
shell python.exe myscript.py 
insheet myfile2.csv, clear 
append using `myfiletemp' 
+0

都能跟得上;'append'增加了觀測(行)垂直您需要添加變量(列)水平與'merge'所以,你需要。創建一個id('generate long id = _n'),將其保存到'.csv'文件中,然後'使用...合併1:1 id' – StasK