Skip to content

Commit

Permalink
Minor edit about tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
davewhipp committed Aug 20, 2024
1 parent 0755b13 commit 39c33d5
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/part1/chapter-03/md/02-data-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ for idx, row in data.iterrows():

We can see that the `idx` variable indeed contains the index value at position 0 (the first row) and the `row` variable contains all the data from that given row stored as a pandas Series. Also, notice that when developing a for loop you do not always need to iterate through the entire loop if you just want to test things out. Using the `break` statement in Python terminates a loop whenever it is placed inside the loop. Here we used it to check out the values on the first row of the DataFrame. This allows us to test the code logic without printing thousands of values to the screen!

Next, let's create an empty column `TEMP_C` for the Celsius temperatures and update the values in that column using the `fahr_to_celsius()` function that we defined earlier. For updating the value in the DataFrame, we can use the `at` method that we already used earlier in this chapter. This time, however, we will use the `itertuples()` method to access the rows in the DataFrame. The `itertuples()` method works similarly to `iterrows()`, except it returns only the row values without the `index`. In addition, the returned values are not a pandas Series, but instead `itertuples()` returns a named tuple data type. As a result, when using `itertuples()` accessing the row values needs to be done a bit differently. A tuple is like a list (but immutable, i.e. you cannot change it) and "named tuple" is a special kind of tuple object that adds the ability to access the values by name instead of position index. Hence, we can access the `TEMP_F` value in a given row using `row.TEMP_F` (in contrast to how we accessed the value in the previous code above). We will not work with named tuples in the rest of the book, but more information can be found in the Python documentation for named tuples [^namedtuple].
Next, let's create an empty column `TEMP_C` for the Celsius temperatures and update the values in that column using the `fahr_to_celsius()` function that we defined earlier. For updating the value in the DataFrame, we can use the `at` method that we already used earlier in this chapter. This time, however, we will use the `itertuples()` method to access the rows in the DataFrame. The `itertuples()` method works similarly to `iterrows()`, except it returns only the row values without the `index`. In addition, the returned values are not a pandas Series, but instead `itertuples()` returns a named tuple data type. As a result, when using `itertuples()` accessing the row values needs to be done a bit differently. Remember, a {term}`tuple` is like a list but {term}`immutable` and a "named tuple" is a special kind of tuple object that adds the ability to access the values by name instead of position index. Hence, we can access the `TEMP_F` value in a given row using `row.TEMP_F` (in contrast to how we accessed the value in the previous code above). We will not work with named tuples in the rest of the book, but more information can be found in the Python documentation for named tuples [^namedtuple].

Let's see an example of how to use the `itertuples()` method.

Expand Down
2 changes: 1 addition & 1 deletion source/part1/chapter-03/nb/02-data-analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@
"source": [
"We can see that the `idx` variable indeed contains the index value at position 0 (the first row) and the `row` variable contains all the data from that given row stored as a pandas Series. Also, notice that when developing a for loop you do not always need to iterate through the entire loop if you just want to test things out. Using the `break` statement in Python terminates a loop whenever it is placed inside the loop. Here we used it to check out the values on the first row of the DataFrame. This allows us to test the code logic without printing thousands of values to the screen!\n",
"\n",
"Next, let's create an empty column `TEMP_C` for the Celsius temperatures and update the values in that column using the `fahr_to_celsius()` function that we defined earlier. For updating the value in the DataFrame, we can use the `at` method that we already used earlier in this chapter. This time, however, we will use the `itertuples()` method to access the rows in the DataFrame. The `itertuples()` method works similarly to `iterrows()`, except it returns only the row values without the `index`. In addition, the returned values are not a pandas Series, but instead `itertuples()` returns a named tuple data type. As a result, when using `itertuples()` accessing the row values needs to be done a bit differently. A tuple is like a list (but immutable, i.e. you cannot change it) and \"named tuple\" is a special kind of tuple object that adds the ability to access the values by name instead of position index. Hence, we can access the `TEMP_F` value in a given row using `row.TEMP_F` (in contrast to how we accessed the value in the previous code above). We will not work with named tuples in the rest of the book, but more information can be found in the Python documentation for named tuples [^namedtuple].\n",
"Next, let's create an empty column `TEMP_C` for the Celsius temperatures and update the values in that column using the `fahr_to_celsius()` function that we defined earlier. For updating the value in the DataFrame, we can use the `at` method that we already used earlier in this chapter. This time, however, we will use the `itertuples()` method to access the rows in the DataFrame. The `itertuples()` method works similarly to `iterrows()`, except it returns only the row values without the `index`. In addition, the returned values are not a pandas Series, but instead `itertuples()` returns a named tuple data type. As a result, when using `itertuples()` accessing the row values needs to be done a bit differently. Remember, a {term}`tuple` is like a list but {term}`immutable` and a \"named tuple\" is a special kind of tuple object that adds the ability to access the values by name instead of position index. Hence, we can access the `TEMP_F` value in a given row using `row.TEMP_F` (in contrast to how we accessed the value in the previous code above). We will not work with named tuples in the rest of the book, but more information can be found in the Python documentation for named tuples [^namedtuple].\n",
"\n",
"Let's see an example of how to use the `itertuples()` method."
]
Expand Down

0 comments on commit 39c33d5

Please sign in to comment.