<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tunde Hammed &#8211; Be on the Right Side of Change</title>
	<atom:link href="https://blog.finxter.com/author/tundehammed/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.finxter.com</link>
	<description></description>
	<lastBuildDate>Fri, 04 Feb 2022 10:33:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.5</generator>

<image>
	<url>https://blog.finxter.com/wp-content/uploads/2020/08/cropped-cropped-finxter_nobackground-32x32.png</url>
	<title>Tunde Hammed &#8211; Be on the Right Side of Change</title>
	<link>https://blog.finxter.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Top 13 Python Tricks for Data Analysis</title>
		<link>https://blog.finxter.com/how-to-use-python-to-analyze-data/</link>
		
		<dc:creator><![CDATA[Tunde Hammed]]></dc:creator>
		<pubDate>Fri, 04 Feb 2022 10:30:37 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Python Built-in Functions]]></category>
		<category><![CDATA[Python One-Liners]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=171639</guid>

					<description><![CDATA[This article focuses on analyzing the coronavirus dataset using Python language. We are not using any of the Python data analysis libraries. Instead, we&#8217;ll use our raw Python skills to write a function, slicing, and indexing. Also, we&#8217;ll use Python arithmetic operators such as sum() and division. Finally, we&#8217;ll use a lambda expression to perform ... <a title="Top 13 Python Tricks for Data Analysis" class="read-more" href="https://blog.finxter.com/how-to-use-python-to-analyze-data/" aria-label="Read more about Top 13 Python Tricks for Data Analysis">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">This article focuses on analyzing the coronavirus dataset using Python language. </p>



<p class="wp-block-paragraph">We are not using any of the Python data analysis libraries. Instead, we&#8217;ll use our raw Python skills to write a function, <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="post" data-id="731" target="_blank">slicing</a>, and <a rel="noreferrer noopener" href="https://blog.finxter.com/daily-python-puzzle-list-indexing/" data-type="post" data-id="84" target="_blank">indexing</a>. </p>



<p class="wp-block-paragraph">Also, we&#8217;ll use Python arithmetic operators such as <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-sum/" data-type="post" data-id="22221" target="_blank">sum()</a></code> and <a rel="noreferrer noopener" href="https://blog.finxter.com/division-in-python/" data-type="post" data-id="16457" target="_blank">division</a>. </p>



<p class="wp-block-paragraph">Finally, we&#8217;ll use a <a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" data-type="post" data-id="2701" target="_blank">lambda expression</a> to perform the traditional looping method.</p>



<p class="wp-block-paragraph">The <a href="https://blog.finxter.com/how-to-install-jupyter-notebook-on-pycharm/" data-type="post" data-id="35442" target="_blank" rel="noreferrer noopener">Jupyter notebook</a> is the preferred IDE (Integrated Development Environment) to write and execute code samples. The dataset we are using is from the data world website. You can download it from the link below.&nbsp;</p>



<ul class="wp-block-list"><li><a href="https://data.world/markmarkoh/coronavirus-data/workspace/file?filename=full_data.csv" target="_blank" rel="noreferrer noopener">https://data.world/markmarkoh/coronavirus-data/workspace/file?filename=full_data.csv</a></li></ul>



<p class="wp-block-paragraph">Our dataset consists of some empty strings. Firstly, we must clean the dataset before performing arithmetic operations or data analysis.</p>



<h2 class="wp-block-heading" id="python-open-and-reader-function">Python open() and reader() Function</h2>



<p class="wp-block-paragraph">We will use <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-read-a-csv-file-into-a-python-list/" data-type="post" data-id="8185" target="_blank">CSV</a> (Comma Separated Values) module to open and read the dataset. The <code>csv</code> module defines the Python <code>reader</code> method and other methods. </p>



<p class="wp-block-paragraph">More on that here <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <a href="https://docs.python.org/3/library/csv.html" target="_blank" rel="noreferrer noopener"><em>https://docs.python.org/3/library/csv.html</em></a><em>. </em></p>



<p class="wp-block-paragraph">Let’s import the <code>reader()</code> function from the python <code>csv</code> module.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from csv import reader</pre>



<p class="wp-block-paragraph">Now, let&#8217;s open and read the coronavirus dataset by running the following code.&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">open_file = open('daily_coronavirus_full_data.csv')
read_file = reader(open_file)
list_covid_file = list(read_file)</pre>



<ul class="wp-block-list"><li>A Python <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793" target="_blank">open()</a></code><strong> </strong>function<strong> </strong>opens a file and returns our datasets into a variable <code>open_file</code>.</li><li>We are using the primary usage of the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-read-a-csv-file-into-a-python-list/" data-type="post" data-id="8185" target="_blank">reader()</a></code> function. A reader reads datasets in the <code>open_file</code> variable.</li><li>And <code>list_covid_file</code> displayed the contents of the dataset as a <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">Python list</a>.</li></ul>



<p class="wp-block-paragraph">Execute the following code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">list_covid_file</pre>



<p class="wp-block-paragraph">Here&#8217;s the output:</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://lh5.googleusercontent.com/Od0TvvBiGayP1v5tMUXvDFYQc3UnhPKYS8BB9uV4Fu9cDZs03meyBLTCOHmXwsfk6ZJBbkqSyxyOarOS-IxEG4BvKqGhbAo_uC0ZxrjImQR0ptajmuJzytJhLvMS7hIctnlZyR0r" alt=""/></figure></div>



<p class="wp-block-paragraph">The above screenshot consists of a <a href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank" rel="noreferrer noopener">list of lists</a>. The first item in the list is the header, followed by the rows of the datasets.</p>



<h3 class="wp-block-heading" id="indexing-and-slicing">Indexing and Slicing</h3>



<p class="wp-block-paragraph">Now, retrieve any row or rows from the dataset using a <code><a href="https://blog.finxter.com/python-slice-a-simple-guide-with-video/" data-type="post" data-id="25095" target="_blank" rel="noreferrer noopener">slice()</a></code> function. Fetch the dataset header with the <code>slice()</code> function.&nbsp;</p>



<p class="wp-block-paragraph"><strong>Code Sample:</strong><strong>&nbsp;&nbsp;&nbsp; </strong>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <strong><em></em></strong></p>



<ul class="wp-block-list"><li><code>list_covid_file[0:1]</code> &#8211; retrieved a dataset contents from index 0 and end at index 1.</li><li>Index 0 is the first row, and index 1 is the second row in the dataset.</li><li>However, the <code>slice()</code> function would ignore the index 1.</li><li>We used Python <code><a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print()</a></code> function to visualise the dataset header as it should in the csv file.&nbsp;</li></ul>



<p class="wp-block-paragraph"><strong>Output</strong><strong><em>:</em></strong></p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://lh6.googleusercontent.com/BiMxDDS9LGgCiUxpB4djSRLDBoEshqyiwk0F4wJRboCJxOt1s6wfBEfSn1yKfeMY6Irbcalkqp7xriF84OAriYFZ_oGPn9YYwRLiiaYKbfkIdYSUcmZgSbWTKYFF0D5-i9eZzaad" alt=""/></figure></div>



<p class="wp-block-paragraph"><em>The above screenshot consists of ten different variables in the dataset head.</em></p>



<p class="wp-block-paragraph"><strong><em>Python Negative Indexing</em></strong>: Get the last row or last element in the list using a <a href="https://blog.finxter.com/daily-python-puzzle-negative-indexing/" data-type="post" data-id="109" target="_blank" rel="noreferrer noopener">negative index</a>.</p>



<p class="wp-block-paragraph"><strong>Code Sample:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(list_covid_file[-1])</pre>



<p class="wp-block-paragraph"><strong>Output</strong>:</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://lh5.googleusercontent.com/ufyd3p_IRKlWA1nZ5wSShP4nuGIMt2mgbNf-WztO_xcNdkP5j2Z-8OdtszsZXXwVTqOGbfSaq59qG6UtgA0ZSkXFtWmAPeNTuCI2kOWZJpjIozRtuIJCx92CRvUvp-VKMbFkcTt7" alt=""/></figure></div>



<h3 class="wp-block-heading" id="using-python-len-function">Using Python len() Function.</h3>



<p class="wp-block-paragraph">The <code><a href="https://blog.finxter.com/python-len/" data-type="post" data-id="22386" target="_blank" rel="noreferrer noopener">len()</a></code> function returns the row number in the datasets. Let’s retrieve the length of our dataset using the <code>len()</code> function.</p>



<p class="wp-block-paragraph"><strong>Run</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">len(covid_dataset)</pre>



<p class="wp-block-paragraph">The Python <code>len()</code> function accepted dataset as a parameter, which returns the following output:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">153482</pre>



<h2 class="wp-block-heading" id="using-list-comprehension">Using List Comprehension</h2>



<p class="wp-block-paragraph"><a href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171" target="_blank" rel="noreferrer noopener">List comprehension</a> returns a new <a href="https://blog.finxter.com/iterators-iterables-and-itertools/" data-type="post" data-id="29507" target="_blank" rel="noreferrer noopener">iterable</a> such as lists, tuples or strings, and it&#8217;s a short version of the traditional looping technique.</p>



<p class="wp-block-paragraph" id="code-sample"><strong>Code Sample:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">get_row = [x for x in covid_dataset]</pre>



<ul class="wp-block-list"><li>We created a variable <code>get_row</code></li><li>List comprehension has two angle brackets that consist of expressions that run each element in the list.</li><li>Then, assign the outcome to the <code>get_row</code> variable.</li></ul>



<p class="wp-block-paragraph">Now, execute <code>get_row</code> variable.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">get_row</pre>



<p class="wp-block-paragraph"><strong>Output</strong></p>



<p class="wp-block-paragraph"><img fetchpriority="high" decoding="async" src="https://lh4.googleusercontent.com/KOZGSqMr4zo6jQQ7StSYvypqKZPCcPFz1LEocyh-XJ3ERDLAPptmPu8GxZpVUfqo0AAUhMbxYiQ0J1-XvWwM8UjPYDFewpvhxXYjbBii4NLTKCUB3YeQBuVmohyc4dt29OCTwCjL" width="602" height="301"></p>



<p class="wp-block-paragraph">You should notice from the above screenshot that we have empty strings (<code>''</code> or <code>'.'</code>) in the dataset. The next task is to replace all the empty strings(<code>' '</code>, <code>'.'</code>) with <code>'0.0'</code>.</p>



<h3 class="wp-block-heading" id="replacing-empty-strings-add-the-result-to-the-list-with-an-append-function">Replacing Empty Strings &#8211; Add the result to the list with an append() function</h3>



<p class="wp-block-paragraph"><strong>Code Sample:</strong></p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://lh6.googleusercontent.com/IF9HY6jlG6NV6zuSRC7byIpgY2iyWDtJTfTayXw3m0B1LCtrEp7Hg_oJRqdBJ4syzwKbjYyms_JWJ3BVQQa129fsuHZKS9E-aQom2EuyIWUkpe3qiEi0GwTQP-GI_RmLz5TMkGwG" alt=""/></figure></div>



<p class="wp-block-paragraph">The above screenshot is a reusable function.</p>



<ul class="wp-block-list"><li>We created a custom function that accepts two parameters: dataset and row.</li><li>And declared an <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-check-if-a-python-list-is-empty/" data-type="post" data-id="9090" target="_blank">empty list</a> <code>fetch_new_data</code><strong>.</strong></li><li>Then iterate over the coronavirus data and assign row into a variable <code>dataset_row</code>.</li><li>We check if the row has empty strings (<code>''</code>, <code>'.'</code>)&nbsp;</li><li>And if it&#8217;s true, assign a value <code>"0.0"</code> to all empty strings.</li><li>Then, we convert the row from the string into a <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-float-function/" data-type="post" data-id="22782" target="_blank">float()</a></code>.</li><li>And add the result<strong><em> </em></strong><code>dataset_row</code> into a list <code>fetch_new_data</code> using Python <code><a href="https://blog.finxter.com/python-list-append/" data-type="post" data-id="6605" target="_blank" rel="noreferrer noopener">append()</a></code><strong><em> </em></strong>function.</li></ul>



<p class="wp-block-paragraph">Outside the loop, return a new list result <code>fetch_new_data</code>.</p>



<p class="wp-block-paragraph">Let’s, create an object of the <code>generic_function</code> function.</p>



<p class="wp-block-paragraph"><strong>Example Code:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">get_dataset = generic_function(covid_dataset, 5)</pre>



<p class="wp-block-paragraph" id="the-generic-function-function-accepts-two-arguments-dataset-and-row-5-which-it-s-assigned-to-a-variable-get-dataset">The <code>generic_function</code> function accepts two arguments: dataset and row 5, which it’s assigned to a variable <code>get_dataset</code><em>.</em></p>



<p class="wp-block-paragraph">Execute:&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">get_dataset</pre>



<p class="wp-block-paragraph"><strong>Output</strong>:&nbsp;</p>



<figure class="wp-block-image is-resized"><img decoding="async" src="https://lh6.googleusercontent.com/q8zxvut5F5XNhD6YLzutNJmfJqtBJBsb1ASjzlGJPXFz5LyCSiBfItsc5Wp64Ged12U8K_IWmytBc1jrnQ-t4vCqZyvlignjUqkIaUce-6MBBeZlVt_i8GbDRNheTWpTEgf40siN" alt="" width="55" height="224"/></figure>



<p class="wp-block-paragraph">We replaced all empty strings with 0.0. We can do this repeatedly by checking any row with empty strings and replacing them with 0.0.</p>



<h2 class="wp-block-heading" id="python-arithmetic-operations">Python Arithmetic Operations</h2>



<h3 class="wp-block-heading" id="using-the-sum-function">Using the sum() function</h3>



<p class="wp-block-paragraph">We will reuse a &#8220;<code>generic_function</code>&#8221; function we created in an earlier example. Add the total number of deaths using a <a href="https://blog.finxter.com/python-built-in-functions/" data-type="post" data-id="17528" target="_blank" rel="noreferrer noopener">built-in</a> Python <code><a href="https://blog.finxter.com/python-sum/" data-type="post" data-id="22221" target="_blank" rel="noreferrer noopener">sum()</a></code> function and return the total of all data points.</p>



<p class="wp-block-paragraph"><strong>Code Sample</strong>:&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">get_all_deaths = sum(get_dataset)</pre>



<ul class="wp-block-list"><li>We created a variable called <code>get_all_deaths</code><em>.</em></li><li>The <code>sum()</code>&nbsp;function accepted <code>get_dataset</code> (object) created from <code>generic_function</code>.</li><li>Then, add all the data points in row 5 and assign it to a variable <code>get_all_deaths</code>.</li></ul>



<p class="wp-block-paragraph">Now run:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">get_all_deaths</pre>



<p class="wp-block-paragraph"><strong>Output</strong>:</p>



<figure class="wp-block-image is-resized"><img decoding="async" src="https://lh4.googleusercontent.com/EVndeA0-4Clx5hxeZufOjqIhHXEXVx4DT6aujkFb2rXB0veZurqKda7o3nz9pdLszgeK5jmQagA4-TaEi_XzkeBYZZx7y014aoLfMtBOSK-z-9TL2ogQff_w17QXaTxhO6a0YGv5" alt="" width="137" height="29"/></figure>



<h3 class="wp-block-heading" id="average-number-of-deaths-using-len-function">Average Number of Deaths Using len() Function</h3>



<p class="wp-block-paragraph">We will compute an average death by dividing the total number of deaths by the total length of row 5 (total death).&nbsp;</p>



<p class="wp-block-paragraph"><strong>Code Sample:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">avg_deaths = get_all_deaths/len(get_dataset)</pre>



<ul class="wp-block-list"><li>We created an <code>avg_deaths</code> variable.</li><li><code>get_all_deaths</code> is the aggregate of all death in row 5 and divided by the length of <code>get_dataset</code> using the Pyhton <code>len()</code> function.</li><li>Then, return the result in a variable <code>avg_death</code>.</li></ul>



<p class="wp-block-paragraph">Let’s execute average deaths:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">avg_death</pre>



<p class="wp-block-paragraph"><strong>Output</strong>:</p>



<figure class="wp-block-image is-resized"><img loading="lazy" decoding="async" src="https://lh3.googleusercontent.com/ofYv9c3UlTe6XfK73tQydGSEzuch0yL43dkcsbEZhPkMgZDloR3kgoVOBmp3dJiV7P7H0WV_fL0uVZaE0P9VC6tDuQRSBjyp_3nDzMYR8F1V0NbXTfnshjmXEx7iYI1BL_WgElH4" alt="" width="201" height="20"/></figure>



<p class="wp-block-paragraph">The above screenshot shows the average number of deaths in all&nbsp;countries.</p>



<h3 class="wp-block-heading" id="python-round-function">Python round() function&nbsp;</h3>



<p class="wp-block-paragraph">Let’s execute:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">round(avg_death, 2)
# 47705.73</pre>



<p class="wp-block-paragraph">Rounded average death into 2 decimal points.</p>



<p class="wp-block-paragraph">Python <code><a href="https://blog.finxter.com/python-round-a-simple-guide-with-video/" data-type="post" data-id="23673" target="_blank" rel="noreferrer noopener">round()</a></code> function returns a floating-point and accepts two parameters; the number to rounded and the decimal place number.&nbsp;</p>



<h2 class="wp-block-heading" id="explore-new-cases-by-country">Explore New Cases By Country</h2>



<h3 class="wp-block-heading" id="python-split-function">Python split() Function</h3>



<p class="wp-block-paragraph">Let&#8217;s continue exploring our dataset by analyzing the number of new cases in each country. </p>



<p class="wp-block-paragraph">Below screenshot is a function that gets new cases of coronavirus by country.</p>



<div class="wp-block-image"><figure class="aligncenter"><img decoding="async" src="https://lh6.googleusercontent.com/8sNoiY9pUlqJVIza8GQ0s5V81v_T1ck5m7A4WrDH8bIdHnxazy6-5QOGzbel_hFmQs9RlAPmvU0WulNLutE_hPDgnVCYB6lx-F6l2RRkF97WrYKEsohj9tz0MO_hTv6Haew_wopi" alt=""/></figure></div>



<p class="wp-block-paragraph">The above function consists of the following:</p>



<ol class="wp-block-list"><li>The function<em> </em><code>new_cases_by_country</code><em> </em>has three parameters: the dataset, locations, and new cases rows.</li><li>We create a variable empty <code>number_of_cases_by_contry</code> that holds the new results list.</li><li>We iterated over the coronavirus dataset and assigned location and new case rows to variables <code>location</code> &amp; <code>new_cases</code>.</li><li>And check if the location is equal to the country in the location row.</li><li>Also, check if the new cases row has empty strings, and if it does, replace them with the value &#8220;0.0&#8221;.</li><li>Concatenate location and <code>new_cases</code> and separate them with a pipe (<code>"|"</code>)sign, which would help us manipulate the result later.</li><li>And add location and new cases into empty list “<code>number_of_cases_by_country</code>” using Python built-in <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-append/" data-type="post" data-id="6605" target="_blank">append()</a></code> function.</li><li>Then, we use a <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-split/" data-type="post" data-id="26097" target="_blank">split()</a></code> function to split the string into a list by splitting the output from the pipe (<code>'|'</code>) sign.</li><li>Then, use a <code>return</code> keyword to return a new list of <code>number_of_cases_by_country</code>.</li></ol>



<p class="wp-block-paragraph">Let&#8217;s create an object of <code>new_cases_by_country</code>.</p>



<p class="wp-block-paragraph"><strong>Sample Code:</strong><em>&nbsp;</em></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">new_cases_by_location = new_cases_by_country(covid_dataset, 1, 2, 'United Kingdom')</pre>



<ol class="wp-block-list"><li>We add three arguments into <code>new_cases_by_country</code> function: dataset,new cases, location rows and country.&nbsp;</li><li>And assigned it to a variable, <code>new_cases_by_location</code>.</li></ol>



<p class="wp-block-paragraph">Execute the following:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">new_cases_by_location</pre>



<p class="wp-block-paragraph"><strong>Output</strong>:</p>



<figure class="wp-block-image is-resized"><img loading="lazy" decoding="async" src="https://lh5.googleusercontent.com/Y4b1vxTNQdBGd4pzUhdQ4zpTCzDgIG1uTstNgqqujudUXtRQgebOpmQQ4d-04-7JizWiv0ptVG_NZs4Oip6mHsnFlOVUnch5jpGzDhHnPZmcQUxUVF3D9uhjuEthtNXsmtef1aqA" alt="" width="288" height="216"/></figure>



<p class="wp-block-paragraph">The above screenshot displayed the number of new cases in the United Kingdom.&nbsp;</p>



<p class="wp-block-paragraph">We can explore the number of cases in each country using the <code>new_cases_by_country</code> function.</p>



<h2 class="wp-block-heading" id="python-lambda-expression">Python Lambda Expression</h2>



<h3 class="wp-block-heading" id="python-map-function">Python map() Function</h3>



<p class="wp-block-paragraph">The <a href="https://blog.finxter.com/python-one-line-for-loop-lambda/" data-type="post" data-id="11312" target="_blank" rel="noreferrer noopener">lambda</a> is an anonymous function that indicates a function without a given name.&nbsp; The following code sample combines python <code><a href="https://blog.finxter.com/python-map/" data-type="post" data-id="242" target="_blank" rel="noreferrer noopener">map()</a></code> and lambda expression to add all the new coronavirus cases in the United Kingdom.&nbsp;</p>



<p class="wp-block-paragraph"><strong>Code Sample:</strong></p>



<figure class="wp-block-image"><img decoding="async" src="https://lh5.googleusercontent.com/WiAeOu9ClHpK5_qLla7opyZE8hwTzQKGIVn_-9w6ijkAsV-SF01jZvRt7cpy2z80GKYUFGzfVvaCvjLfARX-fv_LEhmxPApD2yvFY-HXJBYf0LuxjRUPeqHUapjT_K5WOQiY3gjA" alt=""/></figure>



<ul class="wp-block-list"><li>We introduced an anonymous python function known as <em>lambdas expression</em><strong>. </strong>Check python documentation for more details <a rel="noreferrer noopener" href="https://docs.python.org/3/reference/expressions.html#lambda" target="_blank">6: expressions — Python 3.10.2 documentation</a>.</li><li>We created a variable <code>sum_new_cases</code> that holds all the United Kingdom new cases.</li><li>The <code>map()</code> function makes it simpler and more efficient to iterate over items.</li><li>We pass the lambda function and a list as an argument in a <code>map()</code> function.</li><li>Then, the variable <code>x[1]</code> gets the first index in <code>new_cases_by_location</code> and converts it from string to Python <code>float()</code>, and return a new list result.</li></ul>



<p class="wp-block-paragraph"><strong><em>Output</em></strong>:&nbsp;</p>



<figure class="wp-block-image"><img decoding="async" src="https://lh3.googleusercontent.com/SyGDyR_O9Ggxagb7S5vcjfIzIIZ_jDa7aW8hQvURpAhN--DmfHfeEs1a7G9OBTskIyvTyMmNPEXbkxwOK_wrni6Qq47LZ-OSB0jqsWJ-Gzhyl8krinNgJ4Ui8dZXvlWboxRE5MZH" alt=""/></figure>



<h3 class="wp-block-heading" id="python-max-function">Python max() Function</h3>



<p class="wp-block-paragraph">Python <code><a href="https://blog.finxter.com/python-max/" data-type="post" data-id="21661" target="_blank" rel="noreferrer noopener">max()</a></code> function returns the highest number in the list. </p>



<p class="wp-block-paragraph">Let’s use the <code>max()</code> function to retrieve the highest number of new cases reported in the United Kingdom.</p>



<p class="wp-block-paragraph"><strong>Code Sample:&nbsp;</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">max(sum_new_cases)</pre>



<p class="wp-block-paragraph">The variable <strong>&#8220;</strong><code>sum_new_cases</code><strong>&#8220;</strong> is the lambda expression object we created in the code sample above. </p>



<p class="wp-block-paragraph">We passed the &#8220;<code>sum_new_cases</code><strong>&#8221; </strong>into the <code>max()</code> function, which returns the highest number in the list.</p>



<p class="wp-block-paragraph"><strong>Output</strong>:</p>



<figure class="wp-block-image is-resized"><img loading="lazy" decoding="async" src="https://lh6.googleusercontent.com/k1hj_4NM7YEdX2QDBRC0YCjGGoLQ9fyX6kbiUr9DJC3o8i62k8zUSZFFWgGYNw61JuT2_3cuQQUXYM2LTHX_eohEmDf89X_FbRuBiQU0O7xkopGfZh5FoUoFwkn1HOnvIrUonP6Q" alt="" width="117" height="20"/></figure>



<p class="wp-block-paragraph">The maximum number of cases reported in the United Kingdom is 221222.0</p>



<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>



<p class="wp-block-paragraph">There is a lot of data exploration to cover but, this should furnish you with some primary usage of Python built-in functions, function declaration, and function reusability. </p>



<p class="wp-block-paragraph">All this should come in handy when analyzing a vast dataset. </p>



<p class="wp-block-paragraph">You can go beyond the code samples shown in this article and play around with the dataset to showcase your python skills.&nbsp;</p>






<p class="wp-block-paragraph"></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.finxter.com @ 2026-11-21 11:45:37 by W3 Total Cache
-->