Python End Term Python Code
Python End Term Python Code
ipynb - Colab
# Provides access to system-specific parameters and functions (not directly used here)
import sys
Mounted at /content/drive
# Create a dictionary with keys 'First' and 'Second', pointing to a string and a list, respectively
mydict = {'First': 'Sudarsan', 'Second': [1, 2, 3]}
# Create a 2D NumPy array (matrix) with two rows and three columns
myarray2d = np.array([[100, 200, 300], [400, 500, 600]])
# Create a tuple with four integers; tuples are immutable, meaning their elements cannot be changed
mytuple = (10, 20, 30, 40)
# Access and print various elements from the list and arrays
print(mylist[1]) # Print the second element of the list ('Sudarsan')
print(mylist[2]) # Print the third element of the list (['a', 'b', 'c'])
https://colab.research.google.com/drive/1AIORmRsezAP9eVcc_hTqONPDbEkTO1zK#scrollTo=20W0d4ruQjE4&printMode=true 1/7
8/28/24, 2:51 AM End_Term_Webinar.ipynb - Colab
print(mylist[2][0]) # Print the first element of the nested list within the list ('a')
Sudarsan
['a', 'b', 'c']
a
100
[100 200 300]
100
100
[9, 10, 11]
200
[100, 'Sudarsan']
[100 200]
[['a', 'b', 'c']]
['Sudarsan', ['a', 'b', 'c']]
patientinfo = ['Sudarsan', [76, 132, 37.5], 0] # A list containing a string, a nested list, and an integer
patientinfo[1][0] # Accesses the first element (76) of the nested list [76, 132, 37.5]
76
Pandas series
a = [76, 132, 37.5] # A list containing three elements: Heart Rate (HR), Blood Pressure (BP), and Temperature (Temp)
type(a) # Check the type of the variable 'a' (list)
type(a[0]) # Check the type of the first element in the list 'a' (integer)
# Create a new pandas Series with custom indices ('HR', 'BP', 'Temp') for each corresponding value
mynewseries = pd.Series(a, index=['HR', 'BP', 'Temp'])
print(mynewseries) # Print the new Series with the custom indices
0 76.0
1 132.0
2 37.5
dtype: float64
HR 76.0
BP 132.0
Temp 37.5
dtype: float64
76.0
76.0
<ipython-input-36-efb7a371874f>:16: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version,
print(mynewseries[0]) # Access the first element of the Series by position (returns 76)
Dictionary
https://colab.research.google.com/drive/1AIORmRsezAP9eVcc_hTqONPDbEkTO1zK#scrollTo=20W0d4ruQjE4&printMode=true 2/7
8/28/24, 2:51 AM End_Term_Webinar.ipynb - Colab
# mydict[0] # This line is commented out because it would result in a KeyError (dictionaries are accessed by key, not by index)
# Access the value associated with the key 'First' in the dictionary
mydict['First'] # Returns 'Sudarsan'
# Create a new dictionary with names as keys and lists of measurements as values
newdict = {'Sudarsan': [76, 124, 37.5], 'Priya': [78, 128, 37.2]}
# Loop through the new dictionary, printing each key and its corresponding value
for key, value in newdict.items():
print(key) # Print the key (e.g., 'Sudarsan')
print(value) # Print the value associated with the key (e.g., [76, 124, 37.5])
# Create another dictionary with more complex nested data (lists within lists)
anotherdict = {'Sudarsan': [[76, 124, 37.5], [80, 132, 38]], 'Priya': [[78, 128, 37.2], [82, 131, 38]]}
# Access the first measurement of the first list associated with the key 'Priya'
anotherdict['Priya'][0][0] # Returns 78
Sudarsan
[76, 124, 37.5]
Priya
[78, 128, 37.2]
78
Numpy array
print(X[1, -3:-2]) # Access the second element of the second row, using negative slicing ([7])
print(X[0:2, 2]) # Access the elements in the third column of the first two rows ([6, 4])
print(X[0, 1:]) # Access all elements of the first row starting from the second element ([5, 6, 3])
print(X[0, :-1]) # Access all elements of the first row except the last one ([3, 5, 6])
# listHR > threshold # This would result in an error because comparison operations on lists like this aren't supported
# Filter and print heart rates that are greater than the threshold (75)
print(arrayHR[arrayHR > threshold]) # Print elements of arrayHR that are greater than 75 ([76, 76, 86, 82])
[[ 3 5 6 3]
[ 9 7 4 1]
[-1 0 2 -4]]
3
https://colab.research.google.com/drive/1AIORmRsezAP9eVcc_hTqONPDbEkTO1zK#scrollTo=20W0d4ruQjE4&printMode=true 3/7
8/28/24, 2:51 AM End_Term_Webinar.ipynb - Colab
3
[9 7 4 1]
[7]
[6 4 2]
[6 4]
[5 6 3]
[3 5 6]
[-10 29 -45 54 98 -70]
[29 54 98]
[76 76 86 74 82]
[76 76 86 82]
0
1
2
3
[[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]]
numpy.ndarray
Dataframes
# Attributes of a DataFrame
df_food.shape # Returns a tuple representing the dimensionality of the DataFrame (number of rows, number of columns)
df_food.columns # Returns an Index object containing the column labels of the DataFrame
https://colab.research.google.com/drive/1AIORmRsezAP9eVcc_hTqONPDbEkTO1zK#scrollTo=20W0d4ruQjE4&printMode=true 4/7
8/28/24, 2:51 AM End_Term_Webinar.ipynb - Colab
# Access all rows for the 'Oil' column using the .loc accessor (label-based)
df_food.loc[:, 'Oil']
# Access all rows of the first column (index 0) using the .iloc accessor (position-based)
df_food.iloc[:, 0]
# Access the 'Oil' column, but the result is a DataFrame (not a Series) by using double brackets
df_food[['Oil']]
# Subset the DataFrame to include only the 'Oil' and 'Hardness' columns
df_food[['Oil', 'Hardness']]
# The following line is commented out because the .select method is not available in pandas as it is in R
# df_food.select(['Oil', 'Hardness']) # R-style selection, not applicable in pandas
# Filter the DataFrame to include only the 'Oil' and 'Hardness' columns
df_food.filter(['Oil', 'Hardness'])
# Filter rows where 'Oil' is greater than or equal to 17 and then select 'Oil' and 'Hardness' columns
df_food[df_food['Oil'] >= 17].filter(['Oil', 'Hardness'])
# Access the 'Crispy' column for rows where 'Oil' is greater than or equal to 17 and 'Hardness' is less than or equal to 120
df_food.loc[(df_food['Oil'] >= 17).values & (df_food['Hardness'] <= 120).values, 'Crispy']
B261 13
B264 10
B445 14
B575 8
B665 12
B758 14
B776 13
B836 11
B889 12
B971 13
dtype: int64
https://colab.research.google.com/drive/1AIORmRsezAP9eVcc_hTqONPDbEkTO1zK#scrollTo=20W0d4ruQjE4&printMode=true 5/7
8/28/24, 2:51 AM End_Term_Webinar.ipynb - Colab
# Iterate over each value in the array created by np.arange(5) and print it
for j in np.arange(5):
print(j)
0
1
2
3
4
# Find the maximum value in the 'Oil' column of the df_food DataFrame
max(df_food['Oil'])
# Find the minimum value in the 'Oil' column of the df_food DataFrame
min(df_food['Oil'])
# Filter the DataFrame for rows where 'Crispy' is greater than or equal to 12,
# select the 'Oil' column, and calculate the mean value of the 'Oil' column
np.mean(df_food[df_food['Crispy'] >= 12].filter(['Oil']))
18.014814814814812
https://colab.research.google.com/drive/1AIORmRsezAP9eVcc_hTqONPDbEkTO1zK#scrollTo=20W0d4ruQjE4&printMode=true 6/7
8/28/24, 2:51 AM End_Term_Webinar.ipynb - Colab
https://colab.research.google.com/drive/1AIORmRsezAP9eVcc_hTqONPDbEkTO1zK#scrollTo=20W0d4ruQjE4&printMode=true 7/7