【问题标题】:Calling Python from Stata从Stata调用Python
【发布时间】:2012-10-22 15:35:13
【问题描述】:

这可能很容易,但是在过去几个小时在线查看文档和可能的示例后,我无法弄清楚。

我有一个大型数据集(电子表格),它被 DO 文件大量清理。然后,在 DO 文件中,我想将清理过的数据的某些变量保存为临时 .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.

我一直在手动执行此操作,这很好,但文件将开始快速更改,这将为我节省大量时间。

【问题讨论】:

    标签: python merge python-3.x stata


    【解决方案1】:

    这行得通吗(假设你可以使用 python

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

    【讨论】:

    • 不; append 垂直添加观察值(行)。您需要使用merge 水平添加变量(列)。所以需要创建一个id(generate long id = _n),保存到.csv文件,然后merge 1:1 id using ...
    【解决方案2】:

    在 Stata 中输入“帮助外壳”。您要做的是从 Stata 中脱壳,调用 Python,然后在 Python 脚本完成后让 Stata 恢复您希望它执行的任何操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多