我需要获取一个文件并将信息放在python的数据框中,此过程必须重复20次。有没有办法更快地运行以下所有代码:
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)
....
***嗨!我已经意识到每次都需要单独保存它,因为我需要稍后在time17 = pandas.read_csv(file17, index_col=0, header=0)
上使用它们。是否可以在循环中同时执行该操作和数据帧?非常感谢你!
答案 0 :(得分:1)
尝试一下:
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