-1

I have ran descriptive statistics on a dataset but I have to write them to a CSV and TXT file. Here is my code so far in that section:

dfst=pd.DataFrame(sk)
print (dfst.describe())
dfstt=pd.DataFrame(ku)
print (dfstt.describe())

dfst.describe().to_csv('skewness_stats.csv',index=True)
dfstt.describe().to_csv('kurtosis_stats.csv', index=True)

The DF's run descriptive statistics on Kurtosis and Skewness simulations I ran. I am able to write the statistics to seperate CSV files, but how do I write them to the same one with column headers, etc.? Also unsure how to write them to the same TXT file. Any Ideas?

This is Python 3.10 by the way

  • 3
    did you look at the "IO Tools" section of the pandas documentation? – Paul H Mar 21 '22 at 17:29
  • 1
    The answer is obvious, isn't it? `print(dfst.describe(), file=myfile)`. – Tim Roberts Mar 21 '22 at 17:29
  • @Tim That was my first thought too. There's an existing question about that: [How to redirect 'print' output to a file?](/q/7152762/4518341) – wjandrea Mar 21 '22 at 17:30
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 21 '22 at 18:10

1 Answers1

0

It's unclear if you want the output of describe() or the dataframe as a csv, but anyway, simply use to_csv function of pandas: For instance:

dfstt.to_csv("kurtosis.csv", index=False)
dfstt.describe().to_csv("kurtosis_stats.csv", index=False)

If you want to tune your output, there are plenty of options you can use described there https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html (for instance, if you want the index or not)

Clej
  • 416
  • 3
  • 13