Save .dta files in python

后端 未结 3 614
無奈伤痛
無奈伤痛 2020-12-28 20:24

I\'m wondering if anyone knows a Python package that allows you to save numpy arrays/recarrays in the .dta format of the statistical data analysis software Stat

3条回答
  •  既然无缘
    2020-12-28 20:35

    pandas DataFrame objects now have a "to_stata" method. So you can do for instance

    import pandas as pd
    df = pd.read_stata('my_data_in.dta')
    df.to_stata('my_data_out.dta')
    

    DISCLAIMER: the first step is quite slow (in my test, around 1 minute for reading a 51 MB dta - also see this question), and the second produces a file which can be way larger than the original one (in my test, the size goes from 51 MB to 111MB). This answer may look less elegant, but it is probably more efficient.

提交回复
热议问题