I need to get a file and put the info in a dataframe in python, this process has to be repeated 20 times. is there a way to do this faster intead of runing all the code below:
df = pandas.DataFrame()
file20 = os.path.abspath('Fasting/times/20time.csv')
time20 = pandas.read_csv(file20, index_col=0, header=0)
df['time20'] = time20['Gen1'] + '_'+ time20['Gen2']
file19 = os.path.abspath('Fasting/times/19time.csv')
time19 = pandas.read_csv(file19, index_col=0, header=0)
df['time19'] = time19['Gen1'] + '_'+ time19['Gen2']
file18 = os.path.abspath('Fasting/times/18time.csv')
time18 = pandas.read_csv(file18, index_col=0, header=0)
df['time18'] = time18['Gen1'] + '_'+ time18['Gen2']
file17 = os.path.abspath('Fasting/times/17time.csv')
time17 = pandas.read_csv(file17, index_col=0, header=0)
....
***Hi! I have realized I need each time to be saved it alone, because I need to work with them later on time17 = pandas.read_csv(file17, index_col=0, header=0)
. Is it possible to do that and the dataframe all at the same time in the loop? Thank you very much!
Give this a try:
files = [str(i) + 'time' for i in reversed(range(1, 21))]
pieces = []
# much faster to start with empty list than empty DataFrame
for file in files:
path = 'Fasting/times/%s.csv' % file
frame = pd.read_csv(path, index_col=0, header=0)
pieces.append(frame['Gen1'] + '_' + frame['Gen2'])
df = pd.concat(pieces, axis=1) # may need ignore_index=True
df.columns = files