Convert .CSV files to .DTA files in Python

后端 未结 2 1978
孤街浪徒
孤街浪徒 2021-01-24 11:11

I\'m looking to automate the process of converting many .CSV files into .DTA files via Python. .DTA files is the filetype that is handled by the Stata Statistics language.

2条回答
  •  长发绾君心
    2021-01-24 11:36

    (copypasting from my answer to a previous question)

    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). Spacedman's answer may look less elegant, but it is probably more efficient.

提交回复
热议问题