Skip to main content
`Ctrl`+`K`

# pandas.io.stata.StataWriter.write_file

StataWriter.write_file()

  Export DataFrame object to Stata dta format.

  This method writes the contents of a pandas DataFrame to a .dta file compatible with Stata. It includes features for handling value labels, variable types, and metadata like timestamps and data labels. The output file can then be read and used in Stata or other compatible statistical tools.

  See also

  `read_stata`

    Read Stata file into DataFrame.

  `DataFrame.to_stata`

    Export DataFrame object to Stata dta format.

  `io.stata.StataWriter`

    A class for writing Stata binary dta files.

  Examples

      >>> df = pd.DataFrame(
      ...     {
      ...         "fully_labelled": [1, 2, 3, 3, 1],
      ...         "partially_labelled": [1.0, 2.0, np.nan, 9.0, np.nan],
      ...         "Y": [7, 7, 9, 8, 10],
      ...         "Z": pd.Categorical(["j", "k", "l", "k", "j"]),
      ...     }
      ... )
      >>> path = "/My_path/filename.dta"
      >>> labels = {
      ...     "fully_labelled": {1: "one", 2: "two", 3: "three"},
      ...     "partially_labelled": {1.0: "one", 2.0: "two"},
      ... }
      >>> writer = pd.io.stata.StataWriter(
      ...     path, df, value_labels=labels
      ... )
      >>> writer.write_file()
      >>> df = pd.read_stata(path)
      >>> df
          index fully_labelled  partially_labeled  Y  Z
      0       0            one                one  7  j
      1       1            two                two  7  k
      2       2          three                NaN  9  l
      3       3          three                9.0  8  k
      4       4            one                NaN 10  j