- As jonrsharpe's comment says, read and follow PEP8. Several things to be improved:
You should not import inside a function. All
import
statements should be at the top of your *.py file.Your variable names could be more descriptive. What is
s
? What isfn_list
? (I know I named some of these variables in my answer to your other question, but I was in a hurry and they could definitely use further improvement.
Your functions don't
return
anything and don't take in any parameters. That means your modularization of the code is incomplete. The functions are relying on variables in the global namespace. Yourmain_dataset()
function should probably take in eitherpath
orfilenames
as a parameter, for example, so that you don't have to modify your code to use it on different directories or files. That is, instead ofdef main_dataset():
, you should writedef main_dataset(path):
for example. And it should probably end inreturn df
. That way you don't have loaddf
from the *.csv file again if you are going to use it in downstream analyses.frequency_table()
should probably end inreturn freqDf
(although note thatfreqDf
is not a PEP8-recommend variable name).If you chose, you could make the functions far more versatile by passing many more parameters.
frequency_table()
could take in a path (as a string) to be used for the output file, so it could save results anywhere. It could also take inneuter
andnon_neuter
as parameters. (Those are good variable names BTW!)Rather than having your two functions called explicitly at the bottom of the the code, the python idiom is to nest those calls under a
if __name__ == "__main__":
statement. That way the code is not run if you are justimport
ing your file, but will be run if you call your file from the command line. A very excellent Stack Overflow answerA very excellent Stack Overflow answer has more info on theif __name__ == "__main__":
idiom in python.