如何用一个循环创建多个数据帧?

时间:2019-07-18 22:22:05

标签: python python-3.x pandas

经过数小时与教授的碰撞,我终于能够读取数据文件,导航到我所关注的数据,并使用其中的一些数据构建熊猫数据框。我现在的问题是,我需要这样做30次,但无法弄清楚如何在不覆盖数据和创建新列的同时实现此自动化。

Filename = 'Al_v11.o' #Variable assigned to the file name
File_open = open(Filename,'r') #the r is for read access #opens my MCNP output in memory

Line = File_open.readline()  #Line will now call the command readline, which reads a single line of the MCNP file per time called

counts=0 #1tally appears mutiple times, this allows us to navigate to the second occurance. 
while (Line[0:15] != '1tally        1' or counts < 2): #[0:15] is necessary to constain the while loop, otherwise it will compare 1tally to a whole line and not find a match
    Line = File_open.readline() #this reads a bunch of lines as the while loop iterates
    if Line[0:15] == '1tally        1':
        counts +=1  #we want the second iteration of 1tally
#print (counts)
print (File_open.readline())
print (File_open.readline())
print (File_open.readline())
print (File_open.readline())
print (File_open.readline())
print (File_open.readline()) #the data starts 6 lines below 1tally, thus we need these

myarray= []
while (Line[0:11] != '      total'): #total is the last line, we do not care about it
    Line = File_open.readline()
    if Line[0:11] != '      total': 
        myarray = np.append(myarray, np.fromstring(Line, dtype = float, sep='   ')) #writing data to myarray throughout whileloop

data = np.zeros((int(myarray.size/3),2)) #reshapes the array into a X,2       
data[:,0] = myarray[0:-1:3] #populates the first column
data[:,1] = myarray[1:-1:3] #populates the second column

Foil_1_Front = pd.DataFrame(data,columns=['Energy', 'Counts']) #creates pandas dataframe

我不知道如何遍历我的Energy和Counts的所有60列(每列30个),而又不会每次都覆盖myarray。

0 个答案:

没有答案