Matching line number with string in a table.
Question: I have a file with a list of columns describing particular parameters:

size magnitude luminosity

I need only particular data (in particular lines, and columns) from this file.
So far I have a code in python, where I have appended the necessary line
numbers. I just need to know how i can match it to get the right string in the
text file along with just the variables in columns (magnitude) and
(luminosity.) Any suggestions on how I could approach this?

Here is a sample of my code (#comments describe what I have done and what I
want to do):

    
    
    temp_ListMatch = (point[5]).strip() 
    if temp_ListMatch:
        ListMatchaddress = (point[5]).strip()
        ListMatchaddress = re.sub(r'\s', '_', ListMatchaddress) 
        ListMatch_dirname = '/projects/XRB_Web/apmanuel/499/Lists/' + ListMatchaddress
        #print ListMatch_dirname+"\n" 
    
        try:
            file5 = open(ListMatch_dirname, 'r')
        except IOError:
            print 'Cannot open: '+ListMatch_dirname
    
        Optparline = []
        for line in file5:
            point5 = line.split()
            j = int(point5[1])
            Optparline.append(j)
            #Basically file5 contains the line numbers I need, 
            #and I have appended these numbers to the variable j. 
            temp_others = (point[4]).strip()
            if temp_others: 
                othersaddress = (point[4]).strip()
                othersaddress =re.sub(r'\s', '_', othersaddress) 
                othersbase_dirname = '/projects/XRB_Web/apmanuel/499/Lists/' + othersaddress
                try:
                    file6 = open(othersbase_dirname, 'r')
                except IOError:
                    print 'Cannot open: '+othersbase_dirname
    
                gmag = []
                z = []
                rh = []
                gz = []
    
                for line in file6:
                    point6 = line.split()
                    f = float(point6[2])
                    g = float(point6[4])
                    h = float(point6[6])
                    i = float(point6[9])
             # So now I have opened file 6 where this list of data is, and have
            # identified the columns of elements that I need. 
            # I only need the particular rows (provided by line number) 
            # with these elements chosen. That is where I'm stuck!
    


Answer: Load the whole data file in to a pandas DataFrame (assuming that the data file
has a header from which we can get the column names)

    
    
    import pandas as pd
    df = pd.read_csv('/path/to/file') 
    

Load the file of line numbers into a pandas Series (assuming there's one per
line):

    
    
    #  squeeze = True makes the function return a series
    row_numbers = pd.read_csv('/path/to/rows_file', squeeze = True)
    

Return only those lines which are in the row number file, and the columns
magnitude and luminosity (this assumes that the first row is numbered 0):

    
    
    relevant_rows = df.ix[row_numbers][['magnitude', 'luminosity']
    

