14.
Some Important Commands: pwd and ls
A. How do you know the working directory?
B. How do you know what files are presented in the working directory?
1|Pa ge
15. Writing, Loading and Running Python Files
Method1
A. Create a python file called “Useful_Function” with the following information:
%%writefile Useful_Function.py
def recursion(x):
if(x > 0):
result = x + recursion(x – 1)
print(result)
else:
result = 0
return result
you can check that you created the python file using “ls” command.
ls
You can check also inside the created folder Python Course.
There should
be no space!
2|Pa ge
B. Let us load and run the python file “Useful_Function” in “PythonClass1”?
%load Useful_Function.py
%run Useful_Function.py
Now, simply use the function inside the python file.
print(recursion(6))
NOTE: The python file should be in the same working directory.
3|Pa ge
15. Writing, Loading and Running Python Files
Method2
A. Open a new python file in Juypter Notebook and rename it as “Usefil_Function_File”.
B. Download the file as Python (.py) following the steps below.
C. Move the downloaded file into your Python Course folder.
D. Let us load and run the python file “Useful_Function_File” in “PythonClass1”?
import Useful_Function_File
Now, simply use the function inside the python file.
print(Useful_Function_File.recursion(6))
4|Pa ge
16. Python Modules / Packages
Python Modules/Packages
5|Pa ge
Getting the List of Installed Python Modules / Packages in Anaconda
Step-1: Open the Anaconda Prompt
Step-2: Check the packages installed.
• Use the command that conda list
• Press ENTER
Install Python Module / Package in Anaconda
Step-1: Open the Anaconda Prompt
Step-2: Install the package you want.
• Use the command that pip install package_name
• Press ENTER
EXAMPLE
• Install python package pandas.
• Import the installed package in Juypter Notebook.
6|Pa ge
17. Reading File Using Pandas
Here we use a dataset “RegressionData” about daily counts of bike rentals in Variable Definition
a bike sharing system in Washington DC in the first two years of operation, count daily count of number of bike rentals
year factor with two levels:
matched with climate information of day. The variables of the dataset are
0: 2011
defined in the table to the right. 1: 2012
season factor with four levels:
1→ spring (reference)
2→ summer
NOTE: Copy the text file into the created folder Python Course. 3→ autumn
4→ winter
holiday factor with two levels:
importing the module 0→ not holiday
1→ holiday
import pandas weather factor with three levels:
data = pandas.read_csv("[Link]") 1→ clear to partly cloudy (reference)
2→ mist and/or clouds
print(data)
3→ snow, rain, thunderstorm
temperature normalized temperature in Celsius
using the .read_csv function humidity normalized humidity
windspeed normalized wind speed
printing the file
You can also read the file from your desktop, but you must specify the path.
If the file is not in the same working
import pandas directory, then you need to copy the
data = pandans.read_csv("C:\Users\S_Hil\Desktop\[Link]") file’s path before its name.
7|Pa ge
ALTERNATIVELY
import pandas as pd
importing the module and renaming it
data = pd.read_csv("[Link]")
print(data)
8|Pa ge
The use of [Link] with the general syntax:
.loc[startrow:endrow,startcolumn:endcolumn]
# printing the first row
print([Link][0,])
# printing the first eleven rows
print([Link][0:10,])
9|Pa ge
# printing the “count” column only
print([Link][:,"count"])
# printing the first eleven rows of the “count” column
print([Link][0:10,"count"])
# printing the first eleven rows of the columns from “season” to “weather”
print([Link][0:10,"season":"weather"])
10 | P a g e