0
我需要获取一个文件并将信息放入一个数据框中,这个过程必须重复20次。有没有办法做到这一点运行下面的所有代码更快intent:获取文件并将它们放入一个数据框(优化过程)
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)
之后与他们一起工作。是否有可能在循环中同时执行该操作和数据帧?非常感谢你!
你好,非常感谢你,这几乎是我所需要的。但在新的数据框中,我只需要与文件'df ['timei'] = timei ['Gen1'] +'_'+ timei ['Gen2']'一样多的列。它看起来像是在filei +新列(timei ['Gen1'] +'_'+ timei ['Gen2'])中追加所有列。谢谢!! – Mee
我明白你的意思 - 更新我的回应,试试看。它会为每个文件添加1列。请记住,这是假设您的文件的列是字符串。为了安全起见,您可以使用'df ['Gen1']。astype(str)+'_'+ df ['Gen2']。astype(str)'。 –