Exporting DTA File Using pandas.DataFrame.to_stata() function in Python
Last Updated :
14 Sep, 2021
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
import numpy
import pandas as pd
df = pd.DataFrame({
'person' : [ "Rakesh" , "Kishan" , "Adesh" , "Nitish" ],
'weight' : [ 50 , 60 , 70 , 80 ]
})
display(df)
df.to_stata( 'person.dta' )
|
Output :
Example 2:
Python3
import pandas as pd
df = pd.DataFrame({
'mobiles' : [ "Apple" , "MI" , "Karban" , "JIO" ],
'prizes' : [ 75000 , 9999 , 6999 , 5999 ]
})
display(df)
df.to_stata( 'mobiles.dta' )
|
Output :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...