How do I turn my results into txt, excel or word file?

I have a Python code that does the following: extracts multiple data from tables in distinct excels and performs linear regression between them. So far it's okay but how do I make my results come out clean in a TXT, excel or word file? Thank you

Follows my simplified code because there is much more data:

import numpy as np
import numpy as np
import pandas as pd
import plotly as py
import matplotlib.pyplot as plt
import statsmodels.api as sm
import statsmodels.formula.api as smf

# Carrega a taxa de cambio em Dólares por Real
CAMBIO = pd.read_csv("CAMBIO.csv", parse_dates=["DATE"], index_col="DATE", na_values=".")
CAMBIO['USREAL'] = pd.to_numeric(CAMBIO['USREAL'])
CAMBIO['USREAL'] = CAMBIO.USREAL.shift().apply(np.log) - pd.to_numeric(CAMBIO['USREAL']).apply(np.log)
CAMBIO.columns = ["ReturnsCAMBIO"]
CAMBIO = CAMBIO.asfreq('B')
CAMBIO = CAMBIO.reset_index()


# INICIO DA SEQUENCIA de REGRESSOES

#REGRESSAO DE BBAS3
BBAS3 = pd.read_csv("BBAS3.csv", parse_dates=["Date"], index_col="Date", 
na_values="null")
BBAS3.drop(["Open", "High", "Low", "Adj Close", "Volume"], axis=1, inplace=True)
BBAS3['Close'] = BBAS3.Close.shift().apply(np.log) - 
pd.to_numeric(BBAS3['Close']).apply(np.log)
BBAS3 = BBAS3.interpolate()
BBAS3.columns = ["ReturnsBBAS3"]
BBAS3 = BBAS3.asfreq('B')
BBAS3 = BBAS3.reset_index()
BBAS3 = BBAS3.dropna(axis=0, how='any')


#CONCATENAMOS NA MATRIZ SENSIBILIDADE BBAS3
sensitivityBBAS3 = pd.concat([CAMBIO, BBAS3], axis=1)
sensitivityBBAS3 = sensitivityBBAS3.dropna(axis=0, how='any')

#REALIZAMOS A REGRESSAO COM TODOS FATORES BBAS3
y = sensitivityBBAS3['ReturnsBBAS3']
X = sensitivityBBAS3[["ReturnsCAMBIO"]]
RegressionBBAS3 = sm.OLS(y, X).fit()
print(RegressionBBAS3.summary())

Wanted this RegressionBBAS3 sumamary to be exported to TXT or excel or word.

Author: MullerAlemao, 2018-05-17

1 answers

To create a file there is the function open():

file = open("file.txt","w") 
file.write(RegressionBBAS3.summary())
file.close()
 1
Author: tomasantunes, 2018-05-17 07:30:23