Where possibilities begin

We’re a leading marketplace platform for learning and teaching online. Explore some of our most popular content and learn something new.
Total 4 Results
Introduction to Lists in Python

Created by - Robert Kotaki

Introduction to Lists in Python

Introduction to Lists in PythonIn this chapter and the next, we will learn about lists in Python. Lists are a powerful feature in Python that allow you to store and manipulate sets of information in one place. They are especially useful when dealing with large amounts of data.What is a List?A list is a collection of items in a specific order. You can put anything you want in a list, and the items in a list do not have to be related in any way. In Python, you can create a list using square brackets ([]) and separating the individual items with commas. It's a good idea to use a plural name for the list, such as "bicycles" or "names".Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles) Output:cssCopy code['trek', 'cannondale', 'redline', 'specialized'] Accessing Elements in a ListLists are ordered collections, which means you can access any element in a list by telling Python the position or index of the item you want. In Python, index positions start at 0, not 1.To access an element in a list, write the name of the list followed by the index of the item enclosed in square brackets.Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0]) Output:Copy codetrek You can also use string methods, such as title(), on individual items in the list.Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0].title()) Output:Copy codeTrek Negative index values can also be used to access elements in a list, with -1 representing the last element in the list, -2 representing the second-to-last element, and so on.Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[-1]) Output:Copy codespecialized Using Individual Values from a ListYou can use individual values from a list just like any other variable. For example, you can use f-strings to create a message based on a value from a list.Example:scssCopy codebicycles = ['trek', 'cannondale', 'redline', 'specialized'] message = f"My first bicycle was a {bicycles[0].title()}." print(message) Output:cssCopy codeMy first bicycle was a Trek. Summary:A list is a collection of items in a specific order.Lists are created using square brackets ([]) and separating individual items with commas.Individual items in a list can be accessed using their index position.Index positions start at 0, not 1.Negative index values can be used to access elements from the end of the list.Individual values from a list can be used just like any other variable.

More details

Published - Tue, 11 Apr 2023

MODIFYING, ADDING AND REMOVING ELEMENTS IN LISTS

Created by - Robert Kotaki

MODIFYING, ADDING AND REMOVING ELEMENTS IN LISTS

Lists are dynamic, meaning that you can add, remove or modify elements as needed during the program’s execution. In Python, modifying a list element is done by assigning a new value to an element using the index operator [].This code will output ['ducati', 'yamaha', 'suzuki']. We've changed the first item of the list motorcycles from 'honda' to 'ducati'.To add an element to a list, Python provides several ways to add new data to existing lists.The simplest way to add a new element to a list is to append the item to the list. When you append an item to a list, the new element is added to the end of the list. The append() method is used for this operation.This code will output ['honda', 'yamaha', 'suzuki', 'ducati']. We've added the new element 'ducati' to the end of the list.You can add a new element at any position in your list by using the insert() method. You do this by specifying the index of the new element and the value of the new item.This code will output ['ducati', 'honda', 'yamaha', 'suzuki']. We've inserted the value 'ducati' at the beginning of the list.To remove an item from a list, you can use the del statement or the remove() method.If you know the position of the item you want to remove from a list, you can use the del statement.This code will output ['yamaha', 'suzuki']. We've removed the first item, 'honda', from the list motorcycles.If you know the value of the item you want to remove from a list, you can use the remove() method.This code will output ['honda', 'suzuki']. We've removed the item 'yamaha' from the list motorcycles.

More details

Published - Tue, 11 Apr 2023

ORGANIZING A LIST

Created by - Robert Kotaki

ORGANIZING A LIST

Introduction to ListsSorting a List Permanently with the sort() MethodpythonCopy codecars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars) cars.sort(reverse=True) print(cars) sorted() function lets you display your list in a particular order without affecting the actual order of the list.We can display a list of cars in alphabetical or reverse-alphabetical order temporarily.Example code:Printing a List in Reverse Order with the reverse() MethodpythonCopy codecars = ['bmw', 'audi', 'toyota', 'subaru'] print(cars) cars.reverse() print(cars) len() function returns the length of a list.We can use len() to identify the number of items in a list.Example code:

More details

Published - Tue, 11 Apr 2023

Avoiding Index Errors When Working with Lists

Created by - Robert Kotaki

Avoiding Index Errors When Working with Lists

Avoiding Index Errors When Working with ListsWhen working with lists in Python, it's important to understand how indexing works to avoid index errors. This lecture will cover the basics of indexing in lists and provide some tips to avoid common errors.Indexing in ListsLists are indexed starting at 0, not 1.To access an item in a list, you use square brackets [] and provide the index of the item you want to access.If you provide an index that is out of range for the list, you will get an index error.To access the last item in a list, you can use the index -1.ExamplesLet's create a list of motorcycles and try to access the fourth item:scssCopy codemotorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[3]) This will result in an index error because there is no item at index 3. To fix this error, we need to remember that indexing starts at 0, so the third item in the list is at index 2. We can correct the code by changing the index to 2:scssCopy codemotorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[2]) This will correctly print the third item in the list, 'suzuki'.Let's try to access the last item in the list using the index -1:scssCopy codemotorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[-1]) This will correctly print the last item in the list, 'suzuki'.Finally, let's try to access the last item in an empty list:scssCopy codemotorcycles = [] print(motorcycles[-1]) This will result in an index error because there are no items in the list.Tips to Avoid Index ErrorsRemember that indexing starts at 0, not 1.Use the index -1 to access the last item in a list.If you get an index error, try printing your list or the length of your list to see if it is different than you expected.

More details

Published - Tue, 11 Apr 2023

Search
Popular categories
Latest blogs
Tidyverse Package
Tidyverse Package
Tidyverse PackageThe tidyverse package is a collection of packages for data science in R. It includes some of the most frequently used packages, such as dplyr, ggplot2, readr, and tidyr. By installing and loading the tidyverse package, you can load multiple packages at once.InstallationTo install the tidyverse package, use the following command:RCopy codeinstall.packages("tidyverse") Loading PackagesTo load the tidyverse package, use the following command:RCopy codelibrary(tidyverse) This is equivalent to loading the following packages individually:RCopy codelibrary(ggplot2) library(dplyr) library(tidyr) library(readr) library(purrr) library(tibble) library(stringr) library(forcats) Common Inputs and OutputsAll functions in the tidyverse packages are designed to have common inputs and outputs, which are data frames in "tidy" format. This standardization of input and output data frames makes transitions between different functions in the different packages as seamless as possible.For example, the following code demonstrates the use of dplyr and ggplot2 functions on a data frame:RCopy codelibrary(tidyverse) # create a sample data frame data <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6)) # use dplyr to filter data filtered_data <- data %>% filter(x > 1) # use ggplot2 to create a plot ggplot(filtered_data, aes(x, y)) + geom_point() This code filters the data frame to include only rows where x is greater than 1 and then creates a scatter plot of the remaining data using ggplot2.By using the tidyverse package, you can easily move between different packages and functions, making it a powerful tool for data science in R.Lecture Notes: Introduction to the Tidyverse PackageOverviewThe tidyverse package is an "umbrella" package that installs and loads multiple packages at once for you.The tidyverse package includes some of the most frequently used R packages for data science.The tidyverse package is designed to standardize input and output data frames, making transitions between different functions in the different packages as seamless as possible.Installing and Loading the Tidyverse PackageInstall the tidyverse package using install.packages("tidyverse").Load the tidyverse package using library(tidyverse).scssCopy code# Load individual packages library(dplyr) library(ggplot2) library(readr) library(tidyr) # Load tidyverse package library(tidyverse) Common Packages in the Tidyverse Packageggplot2 for data visualizationdplyr for data wranglingtidyr for converting data to “tidy” formatreadr for importing spreadsheet data into RUsing the Tidyverse PackageUse library(tidyverse) to load all the packages in the tidyverse package.Functions in the tidyverse package have common inputs and outputs: data frames are in "tidy" format.For more information, check out the tidyverse.org webpage for the package.scssCopy code# Load tidyverse package library(tidyverse) # Example of using functions in the tidyverse package data(mpg) mpg %>% filter(class == "subcompact") %>% ggplot() + aes(x = displ, y = hwy, color = manufacturer) + geom_point() ConclusionThe tidyverse package is an essential package for data science in R.It includes multiple packages for data visualization, data wrangling, importing data, and converting data to "tidy" format.Loading the tidyverse package is quicker than loading individual packages.The standardization of input and output data frames makes transitions between different functions in the different packages as seamless as possible.

Sat, 15 Apr 2023

Introduction to nycflights13 package and tidy data
Introduction to nycflights13 package and tidy data
Topic: Introduction to nycflights13 package and tidy dataRecall the nycflights13 package we introduced in Section 1.4 with data about all domestic flights departing from New York City in 2013. The package contains several data frames. Let's take a look at the flights data frame.scssCopy code# Load nycflights13 package library(nycflights13) # View the flights data frame View(flights) We saw that flights has a rectangular shape, with each of its 336,776 rows corresponding to a flight and each of its 22 columns corresponding to different characteristics/measurements of each flight. This satisfied the first two criteria of the definition of “tidy” data from Subsection 4.2.1: that “Each variable forms a column” and “Each observation forms a row.”The nycflights13 package also contains other data frames with their rows representing different observational units:airlines: translation between two letter IATA carrier codes and airline company names (16 in total). The observational unit is an airline company.planes: aircraft information about each of 3,322 planes used. i.e. the observational unit is an aircraft.weather: hourly meteorological data (about 8705 observations) for each of the three NYC airports. i.e. the observational unit is an hourly measurement of weather at one of the three airports.airports: airport names and locations. i.e. the observational unit is an airport.The organization of the information into these five data frames follows the third “tidy” data property: observations corresponding to the same observational unit should be saved in the same table i.e. data frame.Case study: Democracy in GuatemalaIn this section, we’ll show you another example of how to convert a data frame that isn’t in “tidy” format (in other words is in “wide” format) to a data frame that is in “tidy” format (in other words is in “long/narrow” format). We’ll do this using the gather() function from the tidyr package again.Furthermore, we’ll make use of functions from the ggplot2 and dplyr packages to produce a time-series plot showing how the democracy scores have changed over the 40 years from 1952 to 1992 for Guatemala.Let’s use the dem_score data frame we imported in Section 4.1, but focus on only data corresponding to Guatemala.scssCopy code# Load required packages library(dplyr) library(tidyr) library(ggplot2) # Select only data corresponding to Guatemala guat_dem <- dem_score %>% filter(country == "Guatemala") # View guat_dem guat_dem We can see that guat_dem is not in “tidy” format. We need to take the values of the columns corresponding to years in guat_dem and convert them into a new “key” variable called year. Furthermore, we need to take the democracy score values in the inside of the data frame and turn them into a new “value” variable called democracy_score.Our resulting data frame will thus have three columns: country, year, and democracy_score. Recall that the gather() function in the tidyr package can complete this task for us:rCopy codeguat_dem_tidy <- guat_dem %>% gather(key = year, value = democracy_score, -country) # View guat_dem_tidy guat_dem_tidy We set the arguments to gather() as follows:key is the name of the variable in the new data frame that will contain the column names of the original data frame

Sat, 15 Apr 2023

Working with Airline Safety Data
Working with Airline Safety Data
Title: Working with Airline Safety DataIntroduction: In this lecture, we will learn how to work with the airline_safety data frame included in the fivethirtyeight data package. We will explore the data, clean it up, and convert it into a tidy format using R programming language.Step 1: Load the dataset We start by loading the airline_safety dataset using the following command:scssCopy codelibrary(fivethirtyeight) data("airline_safety") Step 2: Exploring the dataset We can use the head() and summary() functions to get a quick overview of the dataset.scssCopy codehead(airline_safety) summary(airline_safety) Step 3: Cleaning the dataset We will remove the incl_reg_subsidiaries and avail_seat_km_per_week columns from the dataset using the select() function from the dplyr package.scssCopy codelibrary(dplyr) airline_safety_smaller <- airline_safety %>% select(-c(incl_reg_subsidiaries, avail_seat_km_per_week)) Step 4: Converting to Tidy Format The current format of the data frame is not tidy. We can convert it to tidy format using the tidyr package.scssCopy codelibrary(tidyr) airline_safety_tidy <- airline_safety_smaller %>% pivot_longer( cols = c( incidents_85_99, fatal_accidents_85_99, fatalities_85_99, incidents_00_14, fatal_accidents_00_14, fatalities_00_14 ), names_to = "incident_type_years", values_to = "count" ) Step 5: Viewing the Tidy Dataset We can use the head() function to view the first few rows of the tidy dataset.scssCopy codehead(airline_safety_tidy) Conclusion: In this lecture, we learned how to work with the airline_safety data frame using R programming language. We explored the dataset, cleaned it up, and converted it to tidy format. The resulting dataset is easier to work with and can be used for further analysis.

Sat, 15 Apr 2023

All blogs