My code is grabbing multiple csv files from a directory, and putting all the data into a dataFrame I created and called "df". Each CSV is the same format, but can be of various lengths so this is what I want to do:
I want to have a column in my df (DataFrame) that records the second to last piece of data in each csv I pull in before it moves onto the next one. I have modified the output below to give you an example of what I mean. Let's suppose I call this column BeforeLast. When you see a 0 value, that means its not the second to last piece of data in the csv I pulled, if you see a 1 value it means its the second to last piece of data in the csv I pulled.
How can I do this as Python is pulling in each csv called upon?
import pandas as pd
import glob
import os
path =r'X:\PublicFiles\TradingData\CSV\RealMarkets\Weekly\Futures\Contracts\Corn C'
allFiles = glob.glob(path + "/*.csv") ##'*' means any file name can be grabbed
df = pd.DataFrame()
list_ = []
for file_ in allFiles:
names = ['Date', 'Open', 'High', 'Low', 'Close', 'Vol', 'OI']
df = pd.read_csv(file_, index_col = None, names = names)
list_.append(df)
frame = pd.concat(list_)
Here is a sample of my current dataFrame (df)
Date Open High Low Close Vol OI
0 20141212 427.00 427.00 427.00 427.00 0 0
1 20141219 429.00 429.00 424.00 424.00 0 0
2 20141226 424.00 425.00 423.00 425.00 0 0
3 20150102 422.75 422.75 417.50 417.50 0 0
This is what I want
Date Open High Low Close Vol OI BeforeLast
0 20141212 427.00 427.00 427.00 427.00 0 0 0
1 20141219 429.00 429.00 424.00 424.00 0 0 0
2 20141226 424.00 425.00 423.00 425.00 0 0 1
3 20150102 422.75 422.75 417.50 417.50 0 0 0 (this is the last piece of data in this csv and now it moves on to the next)
4 20141226 424.00 425.00 423.00 425.00 0 0 0
5 20150102 422.75 422.75 417.50 417.50 0 0 0
6 20141226 424.00 425.00 423.00 425.00 0 0 1
7 20150102 422.75 422.75 417.50 417.50 0 0 0