Python program to rank based on the frequency of names that appears in text files I've written a python program to rank the names that appear in the file(s) based on their frequency. In other words, there are multiple files and want to rank the frequency of the names that appears the most first, then the least at the end. Each name can appear only once in each file. In other words, the same name cannot be repeated in the same file.

Following is the program, that I've written, and wanted to hear your thoughts, on the style/efficiency and tips to improve the same

from pandas import DataFrame
filenames = [ 'Q1.txt', 'Q2.txt' , 'Q3.txt' , 'Q4.txt']
pathToDataFiles = "C:\\DataFiles\\"
outputHTMLFile =pathToDataFiles + "list.html" 
df = DataFrame( columns = ['Name', 'Q1', 'Q2', 'Q3', 'Q4' ])

currentQtr = 1
for filename in filenames:
    entireFilename = pathToDataFiles  + filename
    # Get all the names from the file
    allNames = [line.rstrip('\n') for line in open(entireFilename)]
    # For each name from the latest .txt file
    for eachname in allNames:
        # If the name is in the dataframe then replace the current quarter with 'Y'
        if ( df['Name'].str.contains(eachname).any() ):
           rowNumber = df.loc[df['Name'] == eachname].index[0]
           df.iloc[[rowNumber],[currentQtr]] = 'Y'
        else:
            # It is a new name, thus, replace the current Qtr with 'Y'. Rest with be Nan
            emptyDataFrame =DataFrame( columns = ['Name', 'Q1', 'Q2', 'Q3', 'Q4' ])
            emptyDataFrame = emptyDataFrame.append([{'Name':eachname}])
            rowNumber = 0
            emptyDataFrame.iloc[[rowNumber], [currentQtr]] = 'Y'
            df = df.append(emptyDataFrame)
            df.index = range(len(df))

    currentQtr = currentQtr + 1
# Create a new column with 'Total' occurence of names 
df['Total'] = currentQtr - df.isnull().sum(axis=1) - 1
# Sort by total
df = df.sort_values(by=['Total'], ascending=False)
df.index = range(len(df))
# Fill all Nan with 'X'
df = df.fillna('X')
df.to_html(outputHTMLFile)
I suggest using os.path.join instead of + to handle dangling slashes, when concatenating file names.

This variables:

filenames = [ 'Q1.txt', 'Q2.txt' , 'Q3.txt' , 'Q4.txt']
pathToDataFiles = "C:\\DataFiles\\"


could be better handled by using command line input like in argparse. And for filename you could even use os.listdir to read all files from a directory. Both would give you more flexibility in using this script.

Also, the variable eachname in for eachname in allNames: should be renamed in something like name, to prevent lines like emptyDataFrame = emptyDataFrame.append([{'Name':eachname}]) from looking weird.

There are a few incoherent whitespaces, too, and I think Nan is meant to be NaN (in comments).

Apart from that, it looks like a good start to me. I hope my answer helps and have fun coding!