Skip to content
Related Articles

Related Articles

Improve Article

Exporting DTA File Using pandas.DataFrame.to_stata() function in Python

  • Last Updated : 14 Sep, 2021
Geek Week

This method is used to writes the DataFrame to a Stata dataset file. “dta” files contain a Stata dataset. DTA file is a database file and it is used by IWIS Chain Engineering. 

Syntax : DataFrame.to_stata(path, convert_dates=None, write_index=True, time_stamp=None)

Parameters :

  • path : str, buffer or path object
  • convert_dates : dict
  • write_index : bool
  • time_stamp : datetime

Returns : DataFrame object to Stata dta format. Means return .dta file.

Example 1: Create DTA file



Here we will create dataframe and then saving into the DTA format using DataFrame.to_stata().

Python3




# importing package
import numpy
import pandas as pd
  
# create and view data
df = pd.DataFrame({
    'person': ["Rakesh", "Kishan", "Adesh", "Nitish"],
    'weight': [50, 60, 70, 80]
})
display(df)
  
# use pandas.DataFrame.to_stata method
# to extract .dta file
df.to_stata('person.dta')

Output : 

Example 2:

Python3




# importing package
import pandas as pd
  
# create and view data
df = pd.DataFrame({
    'mobiles': ["Apple", "MI", "Karban", "JIO"],
    'prizes': [75000, 9999, 6999, 5999]
})
display(df)
  
# use pandas.DataFrame.to_stata method
# to extract .dta file
df.to_stata('mobiles.dta')

Output : 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :