Transcription of Introduction to Python Pandas for Data Analytics
1 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionIntroduction to Python Pandas for DataAnalyticsSrijith RajamohanAdvanced Research Computing, Virginia TechTuesday 19thJuly, 20161 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionCourse ContentsThis week: Introduction to Python Python Programming NumPy Plotting with Matplotlib Introduction to Python Pandas Case study Conclusion2 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionSection 11 Introduction to Python2 Python programming3 NumPy4 Matplotlib5 Introduction to Pandas6 Case study7 Conclusion3 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionPython FeaturesWhy Python
2 ? Interpreted Intuitive and minimalistic code Expressive language Dynamically typed Automatic memory management4 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionPython FeaturesAdvantages Ease of programming Minimizes the time to develop and maintain code Modular and object-oriented Large community of users A large standard and user-contributed libraryDisadvantages Interpreted and therefore slower than compiled languages Decentralized with packages5 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionCode Performance vs Development Time6 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionVersions of Python Two versions of Python in use - Python 2 and Python 3 Python 3 not backward-compatible with Python 2 A lot of packages are available for Python 2 Check version using the following commandExample$ Python --version7 / 115 Introductionto
3 PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionSection 21 Introduction to Python2 Python programming3 NumPy4 Matplotlib5 Introduction to Pandas6 Case study7 Conclusion8 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionVariables Variable names can contain alphanumerical characters andsome special characters It is common to have variable names start with alower-case letter and class names start with a capital letter Some keywords are reserved such as and , assert , break , lambda.
4 A list of keywords are located Python is dynamically typed, the type of the variable isderived from the value it is assigned. A variable is assigned using the = operator9 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionVariable types Variable types Integer (int) Float (float) Boolean (bool) Complex (complex) String (str) .. User Defined! (classes) Documentation / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionVariable types Use thetypefunction to determine variable typeExample>>> log_file = open("/home/srijithr/logfile","r")>>> type(log_file)
5 File11 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionVariable types Variables can becastto a different typeExample>>> share_of_rent = / >>> type(share_of_rent)float>>> rounded_share = int(share_of_rent)>>> type(rounded_share)int12 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionOperators Arithmetic operators +, -, *, /, // (integer division forfloating point numbers), ** power Boolean operatorsand,orandnot Comparison operators>,<,>=(greater or equal),<=(less or equal),==equality13 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroductionto PythonPythonprogrammingNumPyMatplotlibIntroductionto PandasCase studyConclusionStrings (str)Example>>> dir(str)[.]
6 , capitalize , center , count , decode , encode , endswith , expandtabs , find , format , index , isalnum , isalpha , isdigit , islower , isspace , istitle , isupper , join , ljust , lower , lstrip , partition , replace , rfind , rindex , rjust , rpartition , rsplit , rstrip , split , splitlines , startswith , strip , swapcase , title , translate , upper , zfill ]14 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionStringsExample>>> greeting = "Hello world!
7 ">>> len(greeting)12>>> greeting Hello world >>> greeting [0] # indexing starts at 0 H >>> ("world", "test")Hello test!15 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionPrinting stringsExample# concatenates strings with a space>>> print("Go", "Hokies")Go Hokies# concatenated without space>>> print("Go" + "Tech" + "Go")GoTechGo# C-style string formatting>>> print("Bar Tab = %f" % )Bar Tab = # Creating a formatted string>>> total = "My Share = %.2f. Tip = %d" %( , )>>> print(total)My Share = Tip = 216 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionListsArray of elements of arbitrary typeExample>>> numbers = [1,2,3]>>> type(numbers)list>>> arbitrary_array = [1,numbers ,"hello"]>>> type(arbitrary_array)
8 List17 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionListsExample# create a new empty list>>> characters = []# add elements using append >>> ("A")>>> ("d")>>> ("d")>>> print(characters)[ A , d , d ]18 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionListsLists aremutable- their values can be >>> characters = ["A","d","d"]# Changing second and third element>>> characters [1] = "p">>> characters [2] = "p">>> print(characters)
9 [ A , p , p ]19 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionListsExample>>> characters = ["A","d","d"]# Inserting before "A","d","d">>> (0, "i")>>> (1, "n")>>> (2, "s")>>> (3, "e")>>> (4, "r")>>> (5, "t")>>>print(characters)[ i , n , s , e , r , t , A , d , d ]20 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionListsExample>>> characters = [ i , n , s , e , r , t , A , d , d ]# Remove first occurrence of "A" from list>>> ("A")>>> print(characters)[ i , n , s , e , r , t , d , d ]# Remove an element at a specific location>>> del characters [7]>>> del characters [6]>>> print(characters)
10 [ i , n , s , e , r , t ]21 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionTuplesTuples are like lists except they areimmutable. Difference is inperformanceExample>>> point = (10, 20) # Note () for tuplesinstead of []>>> type(point)tuple>>> point = 10,20>>> type(point)tuple>>> point [2] = 40 # This will fail!TypeError: tuple object does not supportitem assignment22 / 115 Introductionto PythonPandas forDataAnalyticsSrijithRajamohanIntroduc tionto PythonPythonprogrammingNumPyMatplotlibIn troductionto PandasCase studyConclusionDictionaryDictionaries are lists of key-value pairsExample>>> prices = {"Eggs".}