Sick Gaming
[Tut] Pandas Series Object – A Helpful Guide with Examples - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Pandas Series Object – A Helpful Guide with Examples (/thread-101080.html)



[Tut] Pandas Series Object – A Helpful Guide with Examples - xSicKxBot - 05-01-2023

Pandas Series Object – A Helpful Guide with Examples

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1328553&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Pandas Series Object - A Helpful Guide with Examples&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</p></div>
<p>If you’re working with data in Python, you might have come across the <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="post" data-id="16511" target="_blank">pandas library</a>. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f43c.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p class="has-global-color-8-background-color has-background">One of the key components of pandas is the <strong>Series object</strong>, which is a one-dimensional, labeled array capable of holding data of any type, such as integers, strings, floats, and even Python objects <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f603.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />. </p>
<p>The Series object serves as a foundation for organizing and manipulating data within the pandas library.</p>
<p>This article will teach you more about this crucial data structure and how it can benefit your data analysis workflows. Let’s get started! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2 class="wp-block-heading">Creating a Pandas Series</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-139-1024x682.png" alt="" class="wp-image-1293953" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-139-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/04/image-139-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-139-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2023/04/image-139.png 1168w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>In this section, you’ll learn <strong>how to create a Pandas Series</strong>, a powerful one-dimensional labeled array capable of holding any data type. </p>
<p class="has-global-color-8-background-color has-background">To create a Series, you can use the <code>Series()</code> constructor from the Pandas library. </p>
<p>Make sure you have Pandas installed and imported:</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="">import pandas as pd
</pre>
<p>Now, you can create a Series using the <code>pd.Series()</code> function, and pass in various data structures like <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">lists</a>, <a href="https://blog.finxter.com/python-dictionary/" data-type="post" data-id="5232" target="_blank" rel="noreferrer noopener">dictionaries</a>, or even scalar values. For example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">my_list = [1, 2, 3, 4]
my_series = pd.Series(my_list)
</pre>
<p>The <code>Series()</code> constructor accepts various parameters that help you customize the resulting series, including:</p>
<ul>
<li><code>data</code>: This is the input data—arrays, dicts, or scalars.</li>
<li><code>index</code>: You can provide a custom index for your series to label the values. If you don’t supply one, Pandas will automatically create an integer index (0, 1, 2…).</li>
</ul>
<p>Here’s an example of creating a Series with a custom index:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">custom_index = ['a', 'b', 'c', 'd']
my_series = pd.Series(my_list, index=custom_index)
</pre>
<p>When you create a Series object with a dictionary, Pandas automatically takes the keys as the index and the values as the series data:</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="">my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
my_series = pd.Series(my_dict)
</pre>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Remember</strong>: Your Series can hold various data types, including strings, numbers, and even objects.</p>
<h2 class="wp-block-heading">Pandas Series Indexing</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="607" height="911" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-142.png" alt="" class="wp-image-1293959" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-142.png 607w, https://blog.finxter.com/wp-content/uploads/2023/04/image-142-200x300.png 200w" sizes="(max-width: 607px) 100vw, 607px" /></figure>
</div>
<p>Next, you’ll learn the best ways to index and select data from a Pandas Series, making your data analysis tasks more manageable and enjoyable.</p>
<p class="has-global-color-8-background-color has-background">Again, a <strong>Pandas Series</strong> is a one-dimensional labeled array, and it can hold various data types like integers, floats, and strings. The series object contains an index, which serves multiple purposes, such as metadata identification, automatic and explicit data alignment, and intuitive data retrieval and modification <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f6e0.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>
<p>There are two types of indexing available in a Pandas Series:</p>
<ol>
<li><strong>Position-based indexing</strong> – this uses integer positions to access data. The pandas function <code>iloc[]</code> comes in handy for this purpose.</li>
<li><strong>Label-based indexing</strong> – this uses index labels for data access. The pandas function <code>loc[]</code> works great for this type of indexing.</li>
</ol>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/pandas-series-object-a-helpful-guide-with-examples/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJQBOpbhxQrM%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/pandas-loc-and-iloc-a-simple-guide-with-video/" data-type="URL" data-id="https://blog.finxter.com/pandas-loc-and-iloc-a-simple-guide-with-video/" target="_blank" rel="noreferrer noopener">Pandas <code>loc()</code> and <code>iloc()</code> – A Simple Guide with Video</a></p>
<p>Let’s examine some examples of indexing and selection in a Pandas Series:</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="">import pandas as pd # Sample Pandas Series
data = pd.Series([10, 20, 30, 40, 50], index=['a', 'b', 'c', 'd', 'e']) # Position-based indexing (using iloc)
position_index = data.iloc[2] # Retrieves the value at position 2 (output: 30) # Label-based indexing (using loc)
label_index = data.loc['b'] # Retrieves the value with the label 'b' (output: 20)
</pre>
<p>Keep in mind that while working with Pandas Series, the index labels do not have to be unique but must be <a href="https://blog.finxter.com/python-__hash__-magic-method/" data-type="post" data-id="38118" target="_blank" rel="noreferrer noopener">hashable types</a>. This means they should be of immutable data types like strings, numbers, or <a href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="post" data-id="12043" target="_blank" rel="noreferrer noopener">tuples</a> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f31f.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/mutable-vs-immutable-objects-in-python/" data-type="URL" data-id="https://blog.finxter.com/mutable-vs-immutable-objects-in-python/" target="_blank" rel="noreferrer noopener">Mutable vs. Immutable Objects in Python</a></p>
<h2 class="wp-block-heading">Accessing Values in a Pandas Series</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-146-1024x682.png" alt="" class="wp-image-1293963" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-146-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/04/image-146-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-146-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2023/04/image-146.png 1168w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>So you’re working with Pandas Series and want to access their values. I already showed you this in the previous section but let’s repeat this once again. Repetition. Repetition. Repetition!</p>
<p>First of all, create your Pandas Series:</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="">import pandas as pd data = ['A', 'B', 'C', 'D', 'E']
my_series = pd.Series(data)
</pre>
<p>Now that you have your Series, let’s talk about accessing its values <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />:</p>
<ol>
<li><strong>Using index</strong>: You can access an element in a Series using its index, just like you do with lists:</li>
</ol>
<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="">third_value = my_series[2]
print(third_value) # Output: C
</pre>
<ol start="2">
<li><strong>Using <code>.loc[]</code></strong>: Access an element using its index label with the <code>.loc[]</code> accessor, which is useful when you have custom index names<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f516.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />:</li>
</ol>
<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="">data = ['A', 'B', 'C', 'D', 'E']
index_labels = ['one', 'two', 'three', 'four', 'five']
my_series = pd.Series(data, index=index_labels) second_value = my_series.loc['two']
print(second_value) # Output: B
</pre>
<ol start="3">
<li><strong>Using <code>.iloc[]</code></strong>: Access a value based on its integer position with the <code>.iloc[]</code> accessor. This is particularly helpful when you have non-integer index labels<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f3af.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />:</li>
</ol>
<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="">value_at_position_3 = my_series.iloc[2]
print(value_at_position_3) # Output: C
</pre>
</p>
<h2 class="wp-block-heading">Iterating through a Pandas Series</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="911" height="911" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-143.png" alt="" class="wp-image-1293960" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-143.png 911w, https://blog.finxter.com/wp-content/uploads/2023/04/image-143-300x300.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-143-150x150.png 150w, https://blog.finxter.com/wp-content/uploads/2023/04/image-143-768x768.png 768w" sizes="(max-width: 911px) 100vw, 911px" /></figure>
</div>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Although iterating over a Series is possible, it’s generally discouraged in the Pandas community due to its suboptimal performance. Instead, try using vectorization or other optimized methods, such as <code>apply</code>, <code>transform</code>, or <code>agg</code>. </p>
<p>This section will discuss Series iteration methods, but always remember to consider potential alternatives first!</p>
<p>When you absolutely need to iterate through a Series, you can use the <code>iteritems()</code> function, which returns an iterator of index-value pairs. Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for idx, val in your_series.iteritems(): # Do something with idx and val
</pre>
<p>Another method to iterate over a Pandas Series is by converting it into a list using the <code><a href="https://blog.finxter.com/how-to-convert-a-numpy-array-to-a-python-list/" data-type="post" data-id="908516" target="_blank" rel="noreferrer noopener">tolist()</a></code> function, like this:</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="">for val in your_series.tolist(): # Do something with val
</pre>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> However, keep in mind that these approaches are suboptimal and should be avoided whenever possible. Instead, try one of the following efficient techniques:</p>
<ul>
<li>Vectorized operations: Apply arithmetic or comparison operations directly on the Series.</li>
<li>Use <code><a href="https://blog.finxter.com/the-pandas-apply-function/" data-type="post" data-id="37756" target="_blank" rel="noreferrer noopener">apply()</a></code>: Apply a custom function element-wise.</li>
<li>Use <code><a href="https://blog.finxter.com/pd-agg-aggregating-data-in-pandas/" data-type="post" data-id="443671" target="_blank" rel="noreferrer noopener">agg()</a></code>: Aggregate multiple operations to be applied.</li>
<li>Use <code>transform()</code>: Apply a function and return a similarly-sized Series.</li>
</ul>
<h2 class="wp-block-heading">Sorting a Pandas Series <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f504.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="804" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-141-1024x804.png" alt="" class="wp-image-1293955" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-141-1024x804.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/04/image-141-300x235.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-141-768x603.png 768w, https://blog.finxter.com/wp-content/uploads/2023/04/image-141.png 1161w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p class="has-global-color-8-background-color has-background">Sorting a Pandas Series is pretty straightforward. With the <code><a href="https://blog.finxter.com/pandas-dataframe-sort_values-method/" data-type="post" data-id="343891" target="_blank" rel="noreferrer noopener">sort_values()</a></code> function, you can easily reorder your series, either in ascending or descending order.</p>
<p>First, you must import the Pandas library and create a Pandas Series:</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="">import pandas as pd
s = pd.Series([100, 200, 54.67, 300.12, 400])
</pre>
<p>To sort the values in the series, just use the <code>sort_values()</code> function like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sorted_series = s.sort_values()
</pre>
<p>By default, the values will be sorted in ascending order. If you want to sort them in descending order, just set the <code>ascending</code> parameter to <code>False</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sorted_series = s.sort_values(ascending=False)
</pre>
<p>You can also control the sorting method using the <code>kind</code> parameter. Supported options are <code>'quicksort'</code>, <code>'mergesort'</code>, and <code>'heapsort'</code>. For example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sorted_series = s.sort_values(kind='mergesort')
</pre>
<p>When dealing with <a href="https://blog.finxter.com/pandas-dataframe-missing-data-handling-isna-isnull-notna-notnull-pad-and-replace/" data-type="post" data-id="143381" target="_blank" rel="noreferrer noopener">missing values</a> (<code>NaN</code>) in your series, you can use the <code>na_position</code> parameter to specify their position in the sorted series. The default value is <code>'last'</code>, which places missing values at the end. </p>
<p>To put them at the beginning of the sorted series, just set the <code>na_position</code> parameter to <code>'first'</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="">sorted_series = s.sort_values(na_position='first')</pre>
<h2 class="wp-block-heading">Applying Functions to a Pandas Series</h2>
<p>You might come across situations where you want to apply a custom function to your Pandas Series. Let’s dive into how you can do that using the <code><a href="https://blog.finxter.com/the-pandas-apply-function/" data-type="post" data-id="37756" target="_blank" rel="noreferrer noopener">apply()</a></code> method. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/pandas-series-object-a-helpful-guide-with-examples/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FgkA5wd7RYAU%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p>To begin with, the <code>apply()</code> method is quite flexible and allows you to apply a wide range of functions on your Series. These functions could be NumPy’s universal functions (<code>ufuncs</code>), built-in Python functions, or user-defined functions. Regardless of the type, <code>apply()</code> will work like magic.<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f3a9.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2728.png" alt="✨" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>For instance, let’s say you have a Pandas Series containing square numbers, and you want to find the square root of these numbers:</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="">import pandas as pd square_numbers = pd.Series([4, 9, 16, 25, 36])
</pre>
<p>Now, you can use the <code>apply()</code> method along with the built-in Python function <code>sqrt()</code> to calculate the square root:</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="">import math square_roots = square_numbers.apply(math.sqrt)
print(square_roots)
</pre>
<p>You’ll get the following output:</p>
<pre class="wp-block-preformatted"><code>0 2.0
1 3.0
2 4.0
3 5.0
4 6.0
dtype: float64
</code></pre>
<p>Great job! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f389.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Now, let’s consider you want to create your own function to check if the numbers in a Series are even. Here’s how you can achieve that:</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="">def is_even(number): return number % 2 == 0 even_numbers = square_numbers.apply(is_even)
print(even_numbers)
</pre>
<p>And the output would look like this:</p>
<pre class="wp-block-preformatted"><code>0 True
1 False
2 True
3 False
4 True
dtype: bool
</code></pre>
<p>Congratulations! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f973.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> You’ve successfully used the <code>apply()</code> method with a custom function.</p>
<h2 class="wp-block-heading">Replacing Values in a Pandas Series</h2>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-145-1024x682.png" alt="" class="wp-image-1293962" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-145-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/04/image-145-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-145-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2023/04/image-145.png 1168w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>You might want to replace specific values within a Pandas Series to clean up your data or transform it into a more meaningful format. The <code>replace()</code> function is here to help you do that! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f603.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h3 class="wp-block-heading">How to use <code>replace()</code></h3>
<p class="has-global-color-8-background-color has-background">To use the <code><a href="https://blog.finxter.com/pandas-dataframe-replace-method/" data-type="post" data-id="343842" target="_blank" rel="noreferrer noopener">replace()</a></code> function, simply call it on your Series object like this: <code>your_series.replace(to_replace, value)</code>. <code>to_replace</code> is the value you want to replace, and <code>value</code> is the new value you want to insert instead. You can also use regex for more advanced replacements. </p>
<p>Let’s see an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd data = pd.Series([1, 2, 3, 4])
data = data.replace(2, "Two")
print(data)
</pre>
<p>This code will replace the value <code>2</code> with the string <code>"Two"</code> in your Series. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f504.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h3 class="wp-block-heading">Multiple replacements</h3>
<p>You can replace multiple values simultaneously by passing a dictionary or two lists to the function. For example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">data = pd.Series([1, 2, 3, 4])
data = data.replace({1: 'One', 4: 'Four'})
print(data)
</pre>
<p>In this case, <code>1</code> will be replaced with <code>'One'</code> and <code>4</code> with <code>'Four'</code>. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f389.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h3 class="wp-block-heading">Limiting replacements</h3>
<p>You can limit the number of replacements by providing the <code>limit</code> parameter. For example, if you set <code>limit=1</code>, only the first occurrence of the value will be replaced.</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="">data = pd.Series([2, 2, 2, 2])
data = data.replace(2, "Two", limit=1)
print(data)
</pre>
<p>This code will replace only the first occurrence of <code>2</code> with <code>"Two"</code> in the Series. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2728.png" alt="✨" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2 class="wp-block-heading">Appending and Concatenating Pandas Series</h2>
<p>You might want to combine your pandas Series while working with your data. Worry not! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f603.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Pandas provides easy and convenient ways to append and concatenate your Series.</p>
<h3 class="wp-block-heading">Appending Series</h3>
<p>Appending Series can be done using the <code><a href="https://blog.finxter.com/pandas-dataframe-append-method/" data-type="post" data-id="343998" target="_blank" rel="noreferrer noopener">append()</a></code> method. It allows you to concatenate two or more Series objects. To use it, simply call the method on one series and pass the other series as the argument. </p>
<p>For example:</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="">import pandas as pd series1 = pd.Series([1, 2, 3])
series2 = pd.Series([4, 5, 6]) result = series1.append(series2)
print(result)
</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>0 1
1 2
2 3
0 4
1 5
2 6
dtype: int64</code>
</pre>
<p>However, appending Series iteratively may become computationally expensive. In such cases, consider using <code>concat()</code> instead. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h3 class="wp-block-heading">Concatenating Series</h3>
<p>The <code>concat()</code> function is more efficient when you need to combine multiple Series vertically. Simply provide a list of Series you want to concatenate as its argument, like so:</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="">import pandas as pd series_list = [ pd.Series(range(1, 6), index=list('abcde')), pd.Series(range(1, 6), index=list('fghij')), pd.Series(range(1, 6), index=list('klmno'))
] combined_series = pd.concat(series_list)
print(combined_series)
</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>a 1
b 2
c 3
d 4
e 5
f 1
g 2
h 3
i 4
j 5
k 1
l 2
m 3
n 4
o 5
dtype: int64</code>
</pre>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> There you have it! You’ve combined your Pandas Series using <code>append()</code> and <code>concat()</code>.</p>
<h2 class="wp-block-heading">Renaming a Pandas Series</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="790" height="619" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-340.png" alt="" class="wp-image-1328604" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-340.png 790w, https://blog.finxter.com/wp-content/uploads/2023/04/image-340-300x235.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-340-768x602.png 768w" sizes="(max-width: 790px) 100vw, 790px" /></figure>
</div>
<p>Renaming a Pandas Series is a simple yet useful operation you may need in your data analysis process.</p>
<p class="has-global-color-8-background-color has-background">To start, the <code>rename()</code> method in Pandas can be used to alter the index labels or name of a given Series object. But, if you just want to change the name of the Series, you can set the <code>name</code> attribute directly. For instance, if you have a Series object called <code>my_series</code>, you can rename it to <code>"New_Name"</code> like this:</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="">my_series.name = "New_Name"
</pre>
<p>Now, let’s say you want to rename the index labels of your Series. You can do this using the <code><a href="https://blog.finxter.com/pandas-dataframe-methods-idxmax-idxmin-reindex-reindex_like-rename-rename_axis/" data-type="post" data-id="132331" target="_blank" rel="noreferrer noopener">rename()</a></code> method. Here’s an example:</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="">renamed_series = my_series.rename(index={"old_label1": "new_label1", "old_label2": "new_label2"})
</pre>
<p>The <code>rename()</code> method also accepts functions for more complex transformations. For example, if you want to capitalize all index labels, you can do it like this:</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="">capitalized_series = my_series.rename(index=lambda x: x.capitalize())
</pre>
<p>Keep in mind that the <code>rename()</code> method creates a new Series by default and doesn’t modify the original one. If you want to change the original Series in-place, just set the <code>inplace</code> argument to <code>True</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="">my_series.rename(index={"old_label1": "new_label1", "old_label2": "new_label2"}, inplace=True)</pre>
<h2 class="wp-block-heading">Unique Values in a Pandas Series</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="706" height="941" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-341.png" alt="" class="wp-image-1328605" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-341.png 706w, https://blog.finxter.com/wp-content/uploads/2023/04/image-341-225x300.png 225w" sizes="(max-width: 706px) 100vw, 706px" /></figure>
</div>
<p class="has-global-color-8-background-color has-background">To find unique values in a Pandas Series, you can use the <code>unique()</code> method<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f50d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />. This method returns the unique values in the series without sorting them, maintaining the order of appearance. </p>
<p>Here’s a quick example:</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="">import pandas as pd data = {'A': [1, 2, 1, 4, 5, 4]}
series = pd.Series(data['A']) unique_values = series.unique()
print(unique_values)
</pre>
<p>The output will be: <code>[1, 2, 4, 5]</code></p>
<p>When working with <a rel="noreferrer noopener" href="https://pandas.pydata.org/pandas-docs/stable/user_guide/missing_data.html" target="_blank">missing values</a>, keep in mind that the <code>unique()</code> method includes NaN values if they exist in the series. This behavior ensures you are aware of missing data in your dataset <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4da.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>
<p>If you need to find unique values in multiple columns, the <code>unique()</code> method might not be the best choice, as it only works with Series objects, not DataFrames. Instead, use the <code><a href="https://blog.finxter.com/pandas-dataframe-drop_duplicates-method/" data-type="post" data-id="343624" target="_blank" rel="noreferrer noopener">.drop_duplicates()</a></code> method to get unique combinations of multiple columns.</p>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/data-cleaning-in-python/" data-type="post" data-id="375538" target="_blank" rel="noreferrer noopener">The Ultimate Guide to Data Cleaning in Python and Pandas</a></p>
<p>To summarize, when finding unique values in a Pandas Series:</p>
<ul>
<li>Use the <code>unique()</code> method for a single column <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f9ea.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></li>
<li>Remember that <code>NaN</code> values will be included as unique values when present <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4cc.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></li>
<li>Use the <code>.drop_duplicates()</code> method for multiple columns when needed <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f504.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></li>
</ul>
<p>With these tips, you’re ready to efficiently handle unique values in your Pandas data analysis! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f43c.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4bb.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2 class="wp-block-heading">Converting Pandas Series to Different Data Types</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="790" height="526" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-342.png" alt="" class="wp-image-1328607" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-342.png 790w, https://blog.finxter.com/wp-content/uploads/2023/04/image-342-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-342-768x511.png 768w" sizes="(max-width: 790px) 100vw, 790px" /></figure>
</div>
<p>You can convert a Pandas Series to different data types to modify your data and simplify your work. In this section, you’ll learn how to transform a Series into a DataFrame, List, Dictionary, Array, String, and Numpy Array. Let’s dive in! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h3 class="wp-block-heading">Series to DataFrame</h3>
<p>To convert a Series to a DataFrame, use the <code>to_frame()</code> method. Here’s how:</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="">import pandas as pd data = pd.Series([1, 2, 3, 4])
df = data.to_frame()
print(df)
</pre>
<p>This code will output:</p>
<pre class="wp-block-preformatted"><code> 0
0 1
1 2
2 3
3 4
</code></pre>
<h3 class="wp-block-heading">Series to List</h3>
<p>For transforming a Series to a List, simply call the <code>tolist()</code> method, like this:</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="">data_list = data.tolist()
print(data_list)
</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>[1, 2, 3, 4]</code>
</pre>
<h3 class="wp-block-heading">Series to Dictionary</h3>
<p>To convert your Series into a Dictionary, use the <code>to_dict()</code> method:</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="">data_dict = data.to_dict()
print(data_dict)
</pre>
<p>This results in:</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="">{0: 1, 1: 2, 2: 3, 3: 4}
</pre>
<p>The keys are now indexes, and the values are the original Series data.</p>
<h3 class="wp-block-heading">Series to Array</h3>
<p>Convert your Series to an Array by accessing its <code>.array</code> attribute:</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="">data_array = data.array
print(data_array)
</pre>
<p>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="">&lt;PandasArray>
[1, 2, 3, 4]
</pre>
<h3 class="wp-block-heading">Series to String</h3>
<p>To join all elements of a Series into a single String, use the <code><a href="https://blog.finxter.com/pandas-dataframe-join-method/" data-type="post" data-id="344001" target="_blank" rel="noreferrer noopener">join()</a></code> function from the <code>str</code> library:</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="">data_str = ''.join(map(str, data))
print(data_str)
</pre>
<p>This will result in:</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="">1234
</pre>
<h3 class="wp-block-heading">Series to Numpy Array</h3>
<p>For converting a Series into a Numpy Array, call the <code><a href="https://blog.finxter.com/how-to-convert-pandas-dataframe-series-to-numpy-array/" data-type="post" data-id="821905" target="_blank" rel="noreferrer noopener">to_numpy()</a></code> method:</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="">import numpy as np data_numpy = data.to_numpy()
print(data_numpy)
</pre>
<p>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="">array([1, 2, 3, 4], dtype=int64)
</pre>
<p>Now you’re all set to manipulate your Pandas Series objects and adapt them to different data types! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f389.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2 class="wp-block-heading">Python Pandas Series in Practice <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f43c.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4bb.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="790" height="526" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-343.png" alt="" class="wp-image-1328608" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-343.png 790w, https://blog.finxter.com/wp-content/uploads/2023/04/image-343-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-343-768x511.png 768w" sizes="(max-width: 790px) 100vw, 790px" /></figure>
</div>
<p class="has-global-color-8-background-color has-background">A Pandas Series is a one-dimensional array-like object that’s capable of holding any data type. It’s one of the essential data structures in the Pandas library, along with the <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-create-a-dataframe-from-lists/" data-type="post" data-id="985131" target="_blank">DataFrame</a>. Series is an easy way to organize and manipulate your data, especially when dealing with labeled data, such as SQL databases or dictionary keys. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f511.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/26a1.png" alt="⚡" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>To begin, import the Pandas library, which is usually done with the alias ‘<code>pd</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="">import pandas as pd
</pre>
<h3 class="wp-block-heading">Creating a Pandas Series <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4dd.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f528.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h3>
<p>To create a Series, simply pass a list, ndarray, or dictionary to the <code>pd.Series()</code> function. For example, you can create a Series with integers:</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="">integer_series = pd.Series([1, 2, 3, 4, 5])
</pre>
<p>Or with strings:</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="">string_series = pd.Series(['apple', 'banana', 'cherry'])
</pre>
<p>In case you want your Series to have an explicit index, you can specify the <code>index</code> parameter:</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="">indexed_series = pd.Series(['apple', 'banana', 'cherry'], index=['a', 'b', 'c'])
</pre>
<h3 class="wp-block-heading">Accessing and Manipulating Series Data <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f6aa.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f527.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h3>
<p>Now that you have your Series, here’s how you can access and manipulate the data:</p>
<ul>
<li>Accessing data by index (using both implicit and explicit index):
<ul>
<li>First item: <code>integer_series[0]</code> or <code>indexed_series['a']</code></li>
<li>Slicing: <code>integer_series[1:3]</code></li>
</ul>
</li>
<li>Adding new data:
<ul>
<li>Append: <code>string_series.append(pd.Series(['date']))</code></li>
<li>Add with a label: <code>indexed_series['d'] = 'date'</code></li>
</ul>
</li>
<li>Common Series methods:
<ul>
<li><code><a href="https://blog.finxter.com/pandas-dataframe-all-method/" data-type="post" data-id="343322" target="_blank" rel="noreferrer noopener">all()</a></code> – Check if all elements are true</li>
<li><code><a href="https://blog.finxter.com/pandas-dataframe-any-method/" data-type="post" data-id="343430" target="_blank" rel="noreferrer noopener">any()</a></code> – Check if any elements are true</li>
<li><code>unique()</code> – Get unique values</li>
<li><code>ge(another_series)</code> – Compare elements element-wise with another Series</li>
</ul>
</li>
</ul>
<p>These are just a few examples of interacting with a Pandas Series. There are many other functionalities you can explore! </p>
<p>Practice makes perfect, so feel free to join our free email academy where I’ll show you practical coding projects, data science, exponential technologies in AI and blockchain engineering, Python, and much more. How can you join? Simply download your free cheat sheets by entering your name here:</p>
<p>Let your creativity run wild and happy coding! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f917.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
</div>


https://www.sickgaming.net/blog/2023/04/30/pandas-series-object-a-helpful-guide-with-examples/