如何在csv文件中保存整个数据框?

时间:2016-11-15 22:14:21

标签: python csv

当我尝试执行file.write()时,csv文件中的输出不完整,我有超过3000行数据,我在文件中得到的只有顶部和底部50行。我做错了什么?

PS我在编程方面相当新鲜:)

import pandas as pd
import quandl
import unicodecsv

#getting stock data from Quandl API
df = quandl.get("WIKI/GOOGL")
#defining the columns I want to use
df = df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
#creating new column and calculating using data from previously defined columns
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Close']  * 100

df = df[['Adj. Close', 'HL_PCT']]
print(df)

file = open('adjClosePCT.csv', 'w')
file.write(str(df))

file.close()

1 个答案:

答案 0 :(得分:2)

内置to_csv的pandas应该完成这项工作:

df.to_csv('adjClosePCT.csv')