@@ -6,26 +6,26 @@ questions:
66 - " What types of data can be contained in a DataFrame?"
77 - " Why is the data type important?"
88objectives :
9- - " Describe how information is stored in a Python DataFrame."
10- - " Define the two main types of data in Python : text and numerics."
9+ - " Describe how information is stored in a pandas DataFrame."
10+ - " Define the two main types of data in pandas : text and numerics."
1111 - " Examine the structure of a DataFrame."
1212 - " Modify the format of values in a DataFrame."
1313 - " Describe how data types impact operations."
14- - " Define, manipulate, and interconvert integers and floats in Python."
14+ - " Define, manipulate, and interconvert integers and floats in Python/pandas ."
1515 - " Analyze datasets having missing/null values (NaN values)."
1616 - " Write manipulated data to a file."
1717keypoints :
18- - " Pandas uses other names for data types than Python, for example: `object` for textual data."
18+ - " pandas uses other names for data types than Python, for example: `object` for textual data."
1919 - " A column in a DataFrame can only have one data type."
2020 - " The data type in a DataFrame’s single column can be checked using `dtype`."
2121 - " Make conscious decisions about how to manage missing data."
2222 - " A DataFrame can be saved to a CSV file using the `to_csv` function."
2323---
2424
2525The format of individual columns and rows will impact analysis performed on a
26- dataset read into Python . For example, you can't perform mathematical
26+ dataset read into a pandas DataFrame . For example, you can't perform mathematical
2727calculations on a string (text formatted data). This might seem obvious,
28- however sometimes numeric values are read into Python as strings. In this
28+ however sometimes numeric values are read into pandas as strings. In this
2929situation, when you then try to perform calculations on the string-formatted
3030numeric data, you get an error.
3131
@@ -44,26 +44,26 @@ this lesson: numeric and text data types.
4444Numeric data types include integers and floats. A ** floating point** (known as a
4545float) number has decimal points even if that decimal point value is 0. For
4646example: 1.13, 2.0, 1234.345. If we have a column that contains both integers and
47- floating point numbers, Pandas will assign the entire column to the float data
47+ floating point numbers, pandas will assign the entire column to the float data
4848type so the decimal points are not lost.
4949
5050An ** integer** will never have a decimal point. Thus if we wanted to store 1.13 as
5151an integer it would be stored as 1. Similarly, 1234.345 would be stored as 1234. You
52- will often see the data type ` Int64 ` in Python which stands for 64 bit integer. The 64
52+ will often see the data type ` Int64 ` in pandas which stands for 64 bit integer. The 64
5353refers to the memory allocated to store data in each cell which effectively
5454relates to how many digits it can store in each "cell". Allocating space ahead of time
5555allows computers to optimize storage and processing efficiency.
5656
5757## Text Data Type
5858
59- Text data type is known as Strings in Python, or Objects in Pandas . Strings can
59+ The text data type is known as a * string * in Python, or * object * in pandas . Strings can
6060contain numbers and / or characters. For example, a string might be a word, a
61- sentence, or several sentences. A Pandas object might also be a plot name like
62- 'plot1'. A string can also contain or consist of numbers. For instance, '1234'
63- could be stored as a string, as could '10.23'. However ** strings that contain
61+ sentence, or several sentences. A pandas object might also be a plot name like
62+ ` 'plot1' ` . A string can also contain or consist of numbers. For instance, ` '1234' `
63+ could be stored as a string, as could ` '10.23' ` . However ** strings that contain
6464numbers can not be used for mathematical operations** !
6565
66- Pandas and base Python use slightly different names for data types. More on this
66+ pandas and base Python use slightly different names for data types. More on this
6767is in the table below:
6868
6969| Pandas Type | Native Python Type | Description |
@@ -103,9 +103,9 @@ pandas.core.frame.DataFrame
103103~~~
104104{: .output}
105105
106- Next, let's look at the structure of our surveys data. In pandas, we can check
106+ Next, let's look at the structure of our ` surveys_df ` data. In pandas, we can check
107107the type of one column in a DataFrame using the syntax
108- ` dataFrameName[ column_name].dtype` :
108+ ` dataframe_name[' column_name' ].dtype` :
109109
110110~~~
111111surveys_df['sex'].dtype
@@ -117,7 +117,7 @@ dtype('O')
117117~~~
118118{: .output}
119119
120- A type 'O' just stands for "object" which in Pandas' world is a string
120+ A type 'O' just stands for "object" which in pandas is a string
121121(text).
122122
123123~~~
@@ -130,16 +130,16 @@ dtype('int64')
130130~~~
131131{: .output}
132132
133- The type ` int64 ` tells us that Python is storing each value within this column
134- as a 64 bit integer. We can use the ` dat .dtypes` command to view the data type
133+ The type ` int64 ` tells us that pandas is storing each value within this column
134+ as a 64 bit integer. We can use the ` dataframe_name .dtypes` command to view the data type
135135for each column in a DataFrame (all at once).
136136
137137~~~
138138surveys_df.dtypes
139139~~~
140140{: .language-python}
141141
142- which ** returns** :
142+ which returns:
143143
144144~~~
145145record_id int64
@@ -155,16 +155,16 @@ dtype: object
155155~~~
156156{: .language-python }
157157
158- Note that most of the columns in our Survey data are of type ` int64 ` . This means
159- that they are 64 bit integers. But the weight column is a floating point value
158+ Note that most of the columns in our ` survey_df ` data are of type ` int64 ` . This means
159+ that they are 64 bit integers. But the ` weight ` column is a floating point value
160160which means it contains decimals. The ` species_id ` and ` sex ` columns are objects which
161161means they contain strings.
162162
163163## Working With Integers and Floats
164164
165165So we've learned that computers store numbers in one of two ways: as integers or
166166as floating-point numbers (or floats). Integers are the numbers we usually count
167- with. Floats have fractional parts (decimal places). Let's next consider how
167+ with. Floats have fractional parts (decimal places). Let's next consider how
168168the data type can impact mathematical operations on our data. Addition,
169169subtraction, division and multiplication work on floats and integers as we'd expect.
170170
@@ -264,21 +264,20 @@ dtype('float64')
264264> Try converting the column ` plot_id ` to floats using
265265>
266266> ~~~
267- > surveys_df. plot_id.astype("float")
267+ > surveys_df[' plot_id'] .astype("float")
268268> ~~~
269269> {: .language-python}
270270>
271- > Next try converting `weight` to an integer. What goes wrong here? What is Pandas telling you?
271+ > Next try converting `weight` to an integer. What goes wrong here? What is pandas telling you?
272272> We will talk about some solutions to this later.
273273{: .challenge}
274-
275274## Missing Data Values - NaN
276275
277276What happened in the last challenge activity? Notice that this throws a value error:
278277`ValueError: Cannot convert NA to integer`. If we look at the `weight` column in the surveys
279278data we notice that there are NaN (**N**ot **a** **N**umber) values. **NaN** values are undefined
280- values that cannot be represented mathematically. Pandas , for example, will read
281- an empty cell in a CSV or Excel sheet as a NaN. NaNs have some desirable properties: if we
279+ values that cannot be represented mathematically. pandas , for example, will read
280+ an empty cell in a CSV or Excel sheet as ` NaN` . NaNs have some desirable properties: if we
282281were to average the `weight` column without replacing our NaNs, Python would know to skip
283282over those cells.
284283
@@ -302,26 +301,26 @@ values were handled.
302301For instance, in some disciplines, like Remote Sensing, missing data values are
303302often defined as -9999. Having a bunch of -9999 values in your data could really
304303alter numeric calculations. Often in spreadsheets, cells are left empty where no
305- data are available. Pandas will, by default, replace those missing values with
306- NaN. However it is good practice to get in the habit of intentionally marking
307- cells that have no data, with a no data value! That way there are no questions
304+ data are available. pandas will, by default, replace those missing values with
305+ ` NaN` . However, it is good practice to get in the habit of intentionally marking
306+ cells that have no data with a no data value! That way there are no questions
308307in the future when you (or someone else) explores your data.
309308
310309### Where Are the NaN's?
311310
312- Let's explore the NaN values in our data a bit further. Using the tools we
313- learned in lesson 02, we can figure out how many rows contain NaN values for
314- weight. We can also create a new subset from our data that only contains rows
315- with weight values > 0 (i.e., select meaningful weight values):
311+ Let's explore the ` NaN` values in our data a bit further. Using the tools we
312+ learned in lesson 02, we can figure out how many rows contain ` NaN` values for
313+ ` weight` . We can also create a new subset from our data that only contains rows
314+ with ` weight > 0` (i.e., select meaningful weight values):
316315
317316~~~
318- len(surveys_df[ pd.isnull( surveys_df. weight)] )
317+ len(surveys_df[ surveys_df[ ' weight' ] .isna( )] )
319318# How many rows have weight values?
320- len(surveys_df[ surveys_df. weight > 0] )
319+ len(surveys_df[ surveys_df[ ' weight' ] > 0] )
321320~~~
322321{: .language-python}
323322
324- We can replace all NaN values with zeroes using the `.fillna()` method (after
323+ We can replace all ` NaN` values with zeroes using the `.fillna()` method (after
325324making a copy of the data so we don't lose our work):
326325
327326~~~
@@ -331,8 +330,8 @@ df1['weight'] = df1['weight'].fillna(0)
331330~~~
332331{: .language-python}
333332
334- However NaN and 0 yield different analysis results. The mean value when NaN
335- values are replaced with 0 is different from when NaN values are simply thrown
333+ However ` NaN` and `0` yield different analysis results. The mean value when ` NaN`
334+ values are replaced with `0` is different from when ` NaN` values are simply thrown
336335out or ignored.
337336
338337~~~
@@ -345,37 +344,37 @@ df1['weight'].mean()
345344~~~
346345{: .output}
347346
348- We can fill NaN values with any value that we chose. The code below fills all
349- NaN values with a mean for all weight values.
347+ We can fill ` NaN` values with any value that we chose. The code below fills all
348+ ` NaN` values with a mean for all weight values.
350349
351350~~~
352351df1[ 'weight'] = surveys_df[ 'weight'] .fillna(surveys_df[ 'weight'] .mean())
353352~~~
354353{: .language-python}
355354
356355We could also chose to create a subset of our data, only keeping rows that do
357- not contain NaN values.
356+ not contain ` NaN` values.
358357
359358The point is to make conscious decisions about how to manage missing data. This
360359is where we think about how our data will be used and how these values will
361360impact the scientific conclusions made from the data.
362361
363- Python gives us all of the tools that we need to account for these issues. We
362+ pandas gives us all of the tools that we need to account for these issues. We
364363just need to be cautious about how the decisions that we make impact scientific
365364results.
366365
367366> ## Counting
368367> Count the number of missing values per column.
369368>
370- > > ## Hint
371- > > The method `.count()` gives you the number of non-NA observations per column.
372- > > Try looking to the `.isnull ()` method.
369+ > > ## Hints
370+ > > The method `.count()` gives you the number of non-NaN observations per column.
371+ > > Try looking to the `.isna ()` method.
373372> {: .solution}
374373{: .challenge}
375374
376375## Writing Out Data to CSV
377376
378- We've learned about using manipulating data to get desired outputs. But we've also discussed
377+ We've learned about manipulating data to get desired outputs. But we've also discussed
379378keeping data that has been manipulated separate from our raw data. Something we might be interested
380379in doing is working with only the columns that have full data. First, let's reload the data so
381380we're not mixing up all of our previous manipulations.
@@ -385,7 +384,7 @@ surveys_df = pd.read_csv("data/surveys.csv")
385384~~~
386385{: .language-python}
387386Next, let's drop all the rows that contain missing values. We will use the command `dropna`.
388- By default, dropna removes rows that contain missing data for even just one column.
387+ By default, ` dropna` removes rows that contain missing data for even just one column.
389388
390389~~~
391390df_na = surveys_df.dropna()
@@ -398,7 +397,7 @@ and 9 columns, much smaller than the 35549 row original.
398397We can now use the `to_csv` command to export a DataFrame in CSV format. Note that the code
399398below will by default save the data into the current working directory. We can
400399save it to a different folder by adding the foldername and a slash before the filename:
401- `df.to_csv('foldername/out.csv')`. We use 'index=False' so that
400+ `df.to_csv('foldername/out.csv')`. We use ` 'index=False'` so that
402401pandas doesn't include the index number for each line.
403402
404403~~~
@@ -423,4 +422,3 @@ What we've learned:
423422+ How to use `to_csv` to write manipulated data to a file.
424423
425424{% include links.md %}
426-
0 commit comments