Introduction to Python Programming for Business and Social Science Applications
Реклама. ООО «ЛитРес», ИНН: 7719571260.
Оглавление
Frederick Kaefer. Introduction to Python Programming for Business and Social Science Applications
Introduction to Python Programming for Business and Social Science Applications
Brief Contents
Detailed Contents
Preface. Why Python?
Pedagogy
Approach
Data Sets
Digital Resources
Figures and Tables in the Text Related to the GSS Data Set
Figures and Tables in the Text Related to the Taxi Trips Data Set
Python Modules and Packages
Acknowledgments
About the Authors
1 Introduction to Python. Learning Objectives
Introduction
Brief Introduction to Python and Programming
Python’s Use in Education, Research, and the Corporate World
Python Is Free, Open-Source Software (FOSS)
User Community and Python Resources
Setting Up a Python Development Environment
Python Versions
Executing Python Code in the IDLE Shell Window
Python Insight
Executing Python Code in Files
Stop, Code, and Understand! SCU 1.1 Introducing Stop, Code, and Understand! Exercises
Package Managers
Data Sets Used Throughout the Book
Taxi Trips Data Set
General Social Survey (GSS) Data Set
Chapter Summary
Glossary
End-of-Chapter Exercises
References
Descriptions of Images and Figures
2 Building Blocks of Programming. Learning Objectives
Introduction
Good Programming Practice
Basic Elements of Python Code
Python Keywords
Objects and Classes
Operators and Delimiters
Data Types
Variables
Python Code Statements
Comments
Variable Assignment
Code Examples
Stop, Code, and Understand! SCU 2.1 Variable Assignment
Mathematical Expressions
Stop, Code, and Understand! SCU 2.2 Mathematical Expressions
Errors
Syntax Errors
Stop, Code, and Understand! SCU 2.3 Syntax Errors
Exceptions
Stop, Code, and Understand! SCU 2.4 Exceptions
Logic Errors
Stop, Code, and Understand! SCU 2.5 Logic Errors
Functions. Python Built-in Functions
Stop, Code, and Understand! SCU 2.6 Use a Built-in Function
User-Defined Functions
Function Syntax
Using Functions
Function Location in Code
Stop, Code, and Understand! SCU 2.7 Modify a User-Defined Function
Python Insight
Using Modules of Python Code
Stop, Code, and Understand! SCU 2.8 Using Functions in Modules
Chapter Summary
Glossary
End-of-Chapter Exercises
References
Descriptions of Images and Figures
3 Further Foundations of Python Programming. Learning Objectives
Introduction
Compound Data Types
Lists
Index Values
Stop, Code, and Understand! SCU 3.1 Lists
List Methods
Stop, Code, and Understand! SCU 3.2 Use a List Method
Python Insight
String Objects
String Methods
String Operations
Stop, Code, and Understand! SCU 3.3 String Operations
Sequence Operations
Stop, Code, and Understand! SCU 3.4 Sequence Operations
Tuples
Stop, Code, and Understand! SCU 3.5 Tuple Operations
Dictionaries
Stop, Code, and Understand! SCU 3.6 Reference a Dictionary
Dictionary Operations
Stop, Code, and Understand! SCU 3.7 Dictionary Operations
Example Using Tuples and Dictionaries
Chapter Summary
Glossary
End-of-Chapter Exercises
References
Descriptions of Images and Figures
4 Control Logic and Loops. Learning Objectives
Introduction
Conditions
Conditional Logic. if Statements
Stop, Code, and Understand! SCU 4.1 if Statement
else Statements
Stop, Code, and Understand! SCU 4.2 else Statement
elif Statements
Stop, Code, and Understand! SCU 4.3 elif Statement
Nested if Statements
Stop, Code, and Understand! SCU 4.4 Nested if Statements
Loops
for Statement
Stop, Code, and Understand! SCU 4.5 for Loop
for Loop Example From GSS, Using a Tuple
List Comprehensions
Stop, Code, and Understand! SCU 4.6 List Comprehension
range Function
Stop, Code, and Understand! SCU 4.7 range Function With a for Loop
Python Insight
while Loop
Example: User Entering Multiple Values
Stop, Code, and Understand! SCU 4.8 Use a while Loop
Example: String Processing
Infinite Loops
Error Handling
Handling Exceptions
Stop, Code, and Understand! SCU 4.9 Exception Handling
Logic Error Example
Debugging Python Code
Chapter Summary
Glossary
End-of-Chapter Exercises
Reference
Descriptions of Images and Figures
5 Reading and Writing to Files Using Python. Learning Objectives
Introduction
Data Input/Output: Using Files
Text Files
Stop, Code, and Understand! SCU 5.1 Reading From a Text File
CSV Files
1,000 Taxi Trips
Calculating Summary Statistics on CSV Data
1,000 Survey Results
General Social Survey Example Reading in File With Hours Worked by Age and Year
Using Pandas Module to Read in CSV Files (the “Easy Way”)
Stop, Code, and Understand! SCU 5.2 Read From a CSV File Using the csv Module
Stop, Code, and Understand! SCU 5.3 Read From a CSV File Using Pandas
Python Insight
Exporting Our Results
Stop, Code, and Understand! SCU 5.4 Write to a Text File
Working With Database Files
Retrieving Data From a Database
pyodbc
Connections and Cursors
SQL Using Python
Basic SQL Query
Stop, Code, and Understand! SCU 5.5 SQL Select Query
Filtering
Joining Tables
Stop, Code, and Understand! SCU 5.6 SQL SELECT Query With WHERE Clause
Stop, Code, and Understand! SCU 5.7 SQL Query with Inner Join and where Clauses
SQL Aggregate Functions
Stop, Code, and Understand! SCU 5.8 SQL Query With SUM Function
Stop, Code, and Understand! SCU 5.9 SQL with Group by and having Clauses
Developing an Interactive Application Using a Database
Possible Errors and Their Resolution
Chapter Summary
Glossary
Discussion Questions
End-of-Chapter Exercises
References
Descriptions of Images and Figures
6 Preparing and Working With Data Using Pandas. Learning Objectives
Introduction
NumPy
Stop, Code, and Understand! SCU 6.1 Use NumPy ndarray Properties
Python Insight
Pandas Data Structures
Creating DataFrames and Modifying Their Structure
Stop, Code, and Understand! SCU 6.2 Read in CSV File to Pandas DataFrame and Print Information From DataFrame
Modifying the Structure of a DataFrame
Stop, Code, and Understand! SCU 6.3 Modify Structure of DataFrame Changing Columns
Reporting Information From a DataFrame
Reporting Individual Values From a DataFrame
Stop, Code, and Understand! SCU 6.4 Reporting Information About Data in a Pandas DataFrame
Cleaning and Preparing Data Using Pandas
Removing Records
Recoding Data
Replacing and Recoding Multiple Values in One Step
Stop, Code, and Understand! SCU 6.5 Replacing Multiple Values in One Step
Creating Dummy Variables
Stop, Code, and Understand! SCU 6.6 Creating Dummy Variables
Chapter Summary
Glossary
Discussion Questions
End-of-Chapter Exercises
References
Descriptions of Images and Figures
7 Obtaining Data From the Web Using Python. Learning Objectives
Introduction
HTML: The Language of the Web
HTML Structure
Using Python to Read From HTML Files
Stop, Code, and Understand! SCU 7.1 Print Line From an HTML File
Using Python to Read in Data From HTML Table
Using Pandas to Work With Obtained Data
Stop, Code, and Understand! SCU 7.2 Read an HTML Table
Obtaining GSS Data From the Web: A More Complicated Process
Ethical Issues: Inappropriate Use of Web Resources
Python Insight
BeautifulSoup
Commonly Used BeautifulSoup Features
Using Pandas to Work With Obtained Data
Stop, Code, and Understand! SCU 7.3 Use BeautifulSoup to Read Data From an HTML File
JSON: Obtaining Well-Structured Data
Stop, Code, and Understand! SCU 7.4 Read Data From a JSON File
Taxi Trip Example Using JSON
Stop, Code, and Understand! SCU 7.5 Read JSON Data Using SOCRATA
REST API Queries: A Standardized Way to Access Well-Structured Data
SoQL Parameters
Stop, Code, and Understand! SCU 7.6 Use REST Query
Another API: The Open Library API
Chapter Summary
Glossary
Discussion Questions
End-of-Chapter Exercises
References
Descriptions of Images and Figures
8 Statistical Calculations Using Python. Learning Objectives
Introduction
Ethical Issues: Considerations When Working With Statistics and Building Models
Basic Statistics
Stop, Code, and Understand! SCU 8.1 Use Basic Statistics
Using Statistical Modules
Pandas Features
Correlation Matrix
Stop, Code, and Understand! SCU 8.2 Use Pandas Correlation
SciPy Stats Module
Stop, Code, and Understand! SCU 8.3 Use SciPy
statsmodels Module for Multiple Regression
Stop, Code, and Understand! SCU 8.4 Use statsmodels for Multiple Regression
statsmodels Module for Logistic Regression
Obtaining Data
Stop, Code, and Understand! SCU 8.5 Use statsmodels for Logistic Regression
Python Insight
Chapter Summary
Glossary
End-of-Chapter Exercises
References
Descriptions of Images and Figures
9 Data Visualization Using Python. Learning Objectives
Introduction
Data Visualization
matplotlib: A Python Library to Visualize Your Data
Basic Structure of matplotlib
Stop, Code, and Understand! SCU 9.1 Create Histogram
Bar Charts
Preparing Data for a Bar Chart
Bar Chart Specific Code Instructions
Stop, Code, and Understand! SCU 9.2 Create Bar Chart
Scatterplots
Stop, Code, and Understand! SCU 9.3 Create Scatterplot
Customizing matplotlib Plots
Stop, Code, and Understand! SCU 9.4 Use Custom matplotlib Features
matplotlib Styles
Stop, Code, and Understand! SCU 9.5 Use matplotlib Style
Creating 3D Plots
Interactively Viewing Graphs to Explore a Visualization
Stop, Code, and Understand! SCU 9.6 Create 3D Plot
Using seaborn Package for Statistical Data Visualization
Python Insight
Commonly Used seaborn Functions
seaborn lmplot Function
Stop, Code, and Understand! SCU 9.7 Use seaborn lmplot Function
seaborn distplot Function
Stop, Code, and Understand! SCU 9.8 Use seaborn displot Function
seaborn FacetGrid Function
Stop, Code, and Understand! SCU 9.9 Use seaborn FacetGrid Function
seaborn countplot Function
seaborn heatmap Function
Transforming Data Into Required Formats
Stop, Code, and Understand! SCU 9.10 Customize a Heatmap
Chapter Summary
Glossary
End-of-Chapter Exercises
References
Descriptions of Images and Figures
10 Machine Learning and Text Mining. Learning Objectives
Introduction
Machine Learning
Using the Python scikit-learn Package for Machine Learning
Python Insight
Supervised Learning
Developing Training and Testing Data Sets
Stop, Code, and Understand! SCU 10.1 Create Training/Testing Data Sets
Using Logistic Regression for Classification
Predicting If a Respondent Is Unhappy Based on Marital Status
Using Logistic Regression to Predict If a Respondent Is Widowed
Interpretation of Results
Stop, Code, and Understand! SCU 10.2 Use Logistic Regression for Prediction
Using Neural Networks for Classification
Using a Neural Network Model to Predict If Respondents Are Widowed
Stop, Code, and Understand! SCU 10.3 Use Neural Network for Prediction
Unsupervised Learning
Using k-Means Clustering to Identify Distinct Groups
Scaling Variable Values
Determining the Number of Clusters
k-Means Clustering Example With Taxi Trips Data Set
Stop, Code, and Understand! SCU 10.4 Elbow Method for k-Means
Examining Cluster Information
Stop, Code, and Understand! SCU 10.5 Examining Results From k-Means Clustering
Using Python for Text Mining. Working With Nonnumeric Data
Natural Language Toolkit (nltk)
Language/Sentiment Analysis Using nltk
Sentiment Analysis of Questions in the General Social Survey
Aggregate Analysis of Longer Questions
Stop, Code, and Understand! SCU 10.6 Text Mining
Chapter Summary
Glossary
End-of-Chapter Exercises
References
Descriptions of Images and Figures
11 Developing Graphical User Interfaces With tkinter. Learning Objectives
Introduction
tkinter Background
Stop, Code, and Understand! SCU 11.1 tkinter Main Window
tkinter Widgets
tkinter Layout Manager
Python Insight
Examples Placing Different Widgets
Grid Layout Manager: Configuring Columns and Rows
Creating Widgets
Locating Label Widgets
Locating Entry Widgets
Locating Listbox Widgets
Locating Frame Widgets
Locating Radiobutton Widgets
Locating Button Widgets
Stop, Code, and Understand! SCU 11.2 Place Widget in Main Window
Writing Python Code to Work With tkinter Widgets
Button Widgets
Entry Widgets
Radiobutton Widgets
Listboxes and How to Populate Them
Result When Submit Button Is Clicked
Stop, Code, and Understand! SCU 11.3 Python Code to Work With a Widget
Example Program Using Three tkinter Windows
GUI-Based Database Application
Chapter Summary
Glossary
End-of-Chapter Exercises
References
Descriptions of Images and Figures
Appendix A: Links to Other Resources
Appendix B: Debugging Using IDLE Debug Mode. Debug Mode
Stepping Through Code Statements
Setting Breakpoints
Glossary
Descriptions of Images and Figures
Appendix C: Timing Code Execution
Glossary
Reference
Descriptions of Images and Figures
Appendix D: Solutions to Stop, Code, and Understand! Exercises
Descriptions of Images and Figures
Index
Отрывок из книги
F.K: To my coauthor Paul for all your creative ideas and dedication. It was truly a pleasure working with you. To Jeanne, Stephanie, Matthew, and Conor, for all your love and support.
P.K.: To my coauthor F.K., for your persistence and thoughtful discussion. To Ryan, for your support and distraction when I needed a break.
.....
Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type “quit”.
To get a list of available modules, keywords, symbols, or topics, type “modules”, “keywords”, “symbols”, or “topics”. Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as “spam”, type “modules spam”.
.....