Exporting DTA File Using pandas.DataFrame.to_stata() function in Python
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 packageimport numpyimport pandas as pd # create and view datadf = pd.DataFrame({ 'person': ["Rakesh", "Kishan", "Adesh", "Nitish"], 'weight': [50, 60, 70, 80]})display(df) # use pandas.DataFrame.to_stata method# to extract .dta filedf.to_stata('person.dta') |
Output :


Example 2:
Python3
# importing packageimport pandas as pd # create and view datadf = pd.DataFrame({ 'mobiles': ["Apple", "MI", "Karban", "JIO"], 'prizes': [75000, 9999, 6999, 5999]})display(df) # use pandas.DataFrame.to_stata method# to extract .dta filedf.to_stata('mobiles.dta') |
Output :




Please Login to comment...