在python 2中将数据帧保存为CSV

时间:2015-09-02 14:33:12

标签: python csv pandas

我正在尝试将数据框保存到Csv。当我打印数据帧时,它会生成我正在查找的输出,但是当我将数据帧保存到csv时,我只得到保存到csv文件的数据帧的最后一行..到目前为止我尝试过的......

Index_tickers = pd.read_csv('C:\\Users\\ME\\Dropbox\\MktData\\Index_list\\Index_tickers.csv')
Ticker = Index_tickers.ticker



for ticker in Index_tickers.ticker:    
    index_data = pd.read_csv('C:\\Users\\ME\\Dropbox\\MktData\\Index_list\\' + ticker + '_1.csv')
    mkt_data = index_data[['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']]
    numRow = 2

    while numRow < endRow:
        dOpen0 = mkt_data.ix[numRow, 'Open']
        dHigh0 = mkt_data.ix[numRow, 'High']
        dLow0 = mkt_data.ix[numRow, 'Low']
        dClose0 = mkt_data.ix[numRow, 'Close']
        dDate0 = mkt_data.ix[numRow, 'Date']
        dTime0 = mkt_data.ix[numRow, 'Time']
        dTicker0 = index_data.ix[numRow, 'Ticker']

        dHaClose0 = (dOpen0 + dHigh0 + dLow0 + dClose0) / 4
        dClose1 = mkt_data.ix[numRow -2  , 'Close']
        dOpen1 = mkt_data.ix[numRow -2  , 'Open']
        dHaOpen0 = (dClose1 + dOpen1) / 2 
        dHaHigh0 = max(dHigh0, dHaOpen0, dHaClose0)
        dHaLow0 = min(dLow0, dHaOpen0, dHaClose0)

        dHaGreen0 = dHaClose0 > dHaOpen0
        dHaRed0 = dHaClose0 < dHaOpen0
        dNumRow = numRow        
        numRow = numRow + 1        
        df = pd.DataFrame({'numRow' : pd.Series(dNumRow), 'Time' : pd.Series(dTime0), 'Date' : pd.Series(dDate0), 'Ticker' : pd.Series(dTicker0), 'Open0' : pd.Series(dOpen0), 'High0' : pd.Series(dHigh0), 'Low0' : pd.Series(dLow0), 'Close0' : pd.Series(dClose0)})
        #print df
        df.to_csv('C:\Users\\ME\\Dropbox\\MktData\HaDetail.csv')

非常感谢任何帮助。我是python的新手,在工作中学习..

1 个答案:

答案 0 :(得分:0)

你在每次迭代时都会覆盖你的csv,因为默认的mode'w',如果它存在,它将覆盖,另外你写出你的头,你只需要在第一次迭代时这样做所以我会做以下事情:

Index_tickers = pd.read_csv('C:\\Users\\ME\\Dropbox\\MktData\\Index_list\\Index_tickers.csv')
Ticker = Index_tickers.ticker
writeHeader = True


for ticker in Index_tickers.ticker:    
    index_data = pd.read_csv('C:\\Users\\ME\\Dropbox\\MktData\\Index_list\\' + ticker + '_1.csv')
    mkt_data = index_data[['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']]
    numRow = 2

    while numRow < endRow:
        dOpen0 = mkt_data.ix[numRow, 'Open']
        dHigh0 = mkt_data.ix[numRow, 'High']
        dLow0 = mkt_data.ix[numRow, 'Low']
        dClose0 = mkt_data.ix[numRow, 'Close']
        dDate0 = mkt_data.ix[numRow, 'Date']
        dTime0 = mkt_data.ix[numRow, 'Time']
        dTicker0 = index_data.ix[numRow, 'Ticker']

        dHaClose0 = (dOpen0 + dHigh0 + dLow0 + dClose0) / 4
        dClose1 = mkt_data.ix[numRow -2  , 'Close']
        dOpen1 = mkt_data.ix[numRow -2  , 'Open']
        dHaOpen0 = (dClose1 + dOpen1) / 2 
        dHaHigh0 = max(dHigh0, dHaOpen0, dHaClose0)
        dHaLow0 = min(dLow0, dHaOpen0, dHaClose0)

        dHaGreen0 = dHaClose0 > dHaOpen0
        dHaRed0 = dHaClose0 < dHaOpen0
        dNumRow = numRow        
        numRow = numRow + 1        
        df = pd.DataFrame({'numRow' : pd.Series(dNumRow), 'Time' : pd.Series(dTime0), 'Date' : pd.Series(dDate0), 'Ticker' : pd.Series(dTicker0), 'Open0' : pd.Series(dOpen0), 'High0' : pd.Series(dHigh0), 'Low0' : pd.Series(dLow0), 'Close0' : pd.Series(dClose0)})
        #print df
        if writeHeader:
            df.to_csv('C:\Users\\ME\\Dropbox\\MktData\HaDetail.csv')
            writeHeader = False
        else:
            df.to_csv('C:\Users\\ME\\Dropbox\\MktData\HaDetail.csv', header=False, mode='a')

因此我们只在第一次迭代时编写标头,然后对于每次后续迭代,将mode更改为'a',以便它附加到文件,请参阅docs