Exporting DTA File Using pandas.DataFrame.to_stata() function in Python
Last Updated :
14 Sep, 2021
Improve
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().
# 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:
# 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 :

