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

Asked

Viewed 596 times

0

I have a code in Python that does the following: Extracts several data from different tables and performs a linear regression between them. So far so good but how do I make my results come out clean in a TXT, excel or word file? Thank you

Follow 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 that Regressionbbas3 sumamary to be exported to txt or excel or word.

  • Show the output of Regressionbbas3 to see how the data is, finally with tip (because I have no way to test) try to create a Dataframe with this output and then export to Excel using Pandas. Example: Create Dataframe planilha_final = pd.DataFrame(RegressionBBAS3) | Save to Excel planilha_final.to_excel('planilha_pandas.xlsx', index=False) I hope something I said helps you!

1 answer

1

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

file = open("file.txt","w") 
file.write(RegressionBBAS3.summary())
file.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.