Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit daaebdf

Browse files
committed
rebuilding site Thu, Mar 2, 2023 9:52:11 PM
1 parent ded62b8 commit daaebdf

File tree

7 files changed

+82
-10
lines changed

7 files changed

+82
-10
lines changed

categories/pandas/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<pubDate>Thu, 02 Mar 2023 20:17:18 +0900</pubDate>
1414

1515
<guid>https://thats-it-code.com/pandas/pandas__select-rows-from-a-dataframe-based-on-column-values/</guid>
16-
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method.&lt;/p&gt;
16+
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module.&lt;/p&gt;
1717
&lt;p&gt;&lt;img src=&#34;https://thats-it-code.com/img/pandas_select-rows.png&#34; alt=&#34;Pandas&#34;&gt;&lt;/p&gt;</description>
1818
</item>
1919

index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,18 @@ <h3 style="background-color: rgb(231, 185, 27); padding: 1rem; color: rgb(17, 17
203203
<li><a href="#using-isin">Using isin()</a></li>
204204
<li><a href="#using-loc">Using loc[]</a></li>
205205
<li><a href="#complex-expression">Complex expression</a></li>
206+
<li><a href="#select-by-regular-expression">Select by regular expression</a>
207+
<ul>
208+
<li><a href="#strmatch">str.match()</a></li>
209+
<li><a href="#strcontains">str.contains()</a></li>
210+
<li><a href="#using-re-module">Using re module</a></li>
211+
</ul>
212+
</li>
206213
</ul>
207214
</nav>
208215

209216

210-
<p>In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method.</p>
217+
<p>In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module.</p>
211218
<p><img src="/img/pandas_select-rows.png" alt="Pandas"></p>
212219

213220

index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<pubDate>Thu, 02 Mar 2023 20:17:18 +0900</pubDate>
1717

1818
<guid>https://thats-it-code.com/pandas/pandas__select-rows-from-a-dataframe-based-on-column-values/</guid>
19-
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method.&lt;/p&gt;
19+
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module.&lt;/p&gt;
2020
&lt;p&gt;&lt;img src=&#34;https://thats-it-code.com/img/pandas_select-rows.png&#34; alt=&#34;Pandas&#34;&gt;&lt;/p&gt;</description>
2121
</item>
2222

pandas/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<pubDate>Thu, 02 Mar 2023 20:17:18 +0900</pubDate>
1414

1515
<guid>https://thats-it-code.com/pandas/pandas__select-rows-from-a-dataframe-based-on-column-values/</guid>
16-
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method.&lt;/p&gt;
16+
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module.&lt;/p&gt;
1717
&lt;p&gt;&lt;img src=&#34;https://thats-it-code.com/img/pandas_select-rows.png&#34; alt=&#34;Pandas&#34;&gt;&lt;/p&gt;</description>
1818
</item>
1919

pandas/pandas__select-rows-from-a-dataframe-based-on-column-values/index.html

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919

2020
</title>
21-
<meta name="description" content="In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method. ">
21+
<meta name="description" content="In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module. ">
2222
<meta itemprop="name" content="Pandas &gt;&gt; Select Rows From a DataFrame Based on Column Values">
23-
<meta itemprop="description" content="In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method. ">
23+
<meta itemprop="description" content="In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module. ">
2424
<meta property="og:title" content="Pandas &gt;&gt; Select Rows From a DataFrame Based on Column Values">
25-
<meta property="og:description" content="In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method. ">
25+
<meta property="og:description" content="In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module. ">
2626
<meta property="og:image" content="https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?size=200">
2727
<meta property="og:url" content="https://thats-it-code.com/pandas/pandas__select-rows-from-a-dataframe-based-on-column-values/">
2828
<meta property="og:site_name" content="That&#39;s it ! Code Snippets">
@@ -185,12 +185,19 @@ <h2>Table of Contents</h2>
185185
<li><a href="#using-isin">Using isin()</a></li>
186186
<li><a href="#using-loc">Using loc[]</a></li>
187187
<li><a href="#complex-expression">Complex expression</a></li>
188+
<li><a href="#select-by-regular-expression">Select by regular expression</a>
189+
<ul>
190+
<li><a href="#strmatch">str.match()</a></li>
191+
<li><a href="#strcontains">str.contains()</a></li>
192+
<li><a href="#using-re-module">Using re module</a></li>
193+
</ul>
194+
</li>
188195
</ul>
189196
</nav>
190197

191198

192199

193-
<p>In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method.</p>
200+
<p>In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module.</p>
194201
<p><img src="/img/pandas_select-rows.png" alt="Pandas"></p>
195202
<h2 id="basic">Basic</h2>
196203
<p>To select rows from a Pandas DataFrame based on column values, you can use boolean indexing. Here&rsquo;s an example:</p>
@@ -268,6 +275,64 @@ <h2 id="complex-expression">Complex expression</h2>
268275
<span style="color:#75715e"># Display the result</span>
269276
print(result)
270277
</code></pre></div><p>In this example, we define a complex condition to select rows based on whether the &lsquo;Name&rsquo; column contains the letter &lsquo;a&rsquo;, the &lsquo;Age&rsquo; column is greater than or equal to 30, and either the &lsquo;Gender&rsquo; column is &lsquo;M&rsquo; OR the &lsquo;Salary&rsquo; column is greater than or equal to 80000. We use the str.contains() method to check whether the &lsquo;Name&rsquo; column contains the letter &lsquo;a&rsquo;. We combine the conditions using the &amp; and | operators and enclose each condition in parentheses to specify the order of operations. The resulting DataFrame is stored in the result variable and printed to the console.</p>
278+
<h2 id="select-by-regular-expression">Select by regular expression</h2>
279+
<h3 id="strmatch">str.match()</h3>
280+
<p>The str.match() method in Pandas is used to extract only the rows from a DataFrame where the values in a particular column match a regular expression pattern.</p>
281+
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> pandas <span style="color:#66d9ef">as</span> pd
282+
283+
<span style="color:#75715e"># Create a sample DataFrame</span>
284+
df <span style="color:#f92672">=</span> pd<span style="color:#f92672">.</span>DataFrame({
285+
<span style="color:#e6db74">&#39;Name&#39;</span>: [<span style="color:#e6db74">&#39;Alice&#39;</span>, <span style="color:#e6db74">&#39;Bob&#39;</span>, <span style="color:#e6db74">&#39;Charlie&#39;</span>, <span style="color:#e6db74">&#39;Dave&#39;</span>, <span style="color:#e6db74">&#39;Eva&#39;</span>],
286+
<span style="color:#e6db74">&#39;Age&#39;</span>: [<span style="color:#ae81ff">25</span>, <span style="color:#ae81ff">30</span>, <span style="color:#ae81ff">35</span>, <span style="color:#ae81ff">40</span>, <span style="color:#ae81ff">45</span>],
287+
<span style="color:#e6db74">&#39;City&#39;</span>: [<span style="color:#e6db74">&#39;New York&#39;</span>, <span style="color:#e6db74">&#39;Los Angeles&#39;</span>, <span style="color:#e6db74">&#39;Chicago&#39;</span>, <span style="color:#e6db74">&#39;San Francisco&#39;</span>, <span style="color:#e6db74">&#39;Seattle&#39;</span>]
288+
})
289+
290+
<span style="color:#75715e"># Select rows where the &#39;City&#39; column starts with the letter &#39;N&#39;</span>
291+
result <span style="color:#f92672">=</span> df[df[<span style="color:#e6db74">&#39;City&#39;</span>]<span style="color:#f92672">.</span>str<span style="color:#f92672">.</span>match(<span style="color:#e6db74">&#39;^N&#39;</span>)]
292+
293+
<span style="color:#75715e"># Display the result</span>
294+
print(result)
295+
</code></pre></div><p>In this example, we use the str.match() method to select rows where the &lsquo;City&rsquo; column starts with the letter &lsquo;N&rsquo;. We pass the regular expression pattern &lsquo;^N&rsquo; as an argument to the method to match only strings that start with the letter &lsquo;N&rsquo;. The resulting DataFrame is stored in the result variable and printed to the console.</p>
296+
<p>Note that the str.match() method only matches patterns at the beginning of the string. If you want to match patterns anywhere in the string, you should use the str.contains() method instead. Also, like with str.contains(), you can adjust the regular expression pattern to fit your specific needs and criteria.</p>
297+
<h3 id="strcontains">str.contains()</h3>
298+
<p>To select rows in a Pandas DataFrame using regular expressions (regex), you can use the str.contains() method.</p>
299+
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> pandas <span style="color:#66d9ef">as</span> pd
300+
301+
<span style="color:#75715e"># Create a sample DataFrame</span>
302+
df <span style="color:#f92672">=</span> pd<span style="color:#f92672">.</span>DataFrame({
303+
<span style="color:#e6db74">&#39;Name&#39;</span>: [<span style="color:#e6db74">&#39;Alice&#39;</span>, <span style="color:#e6db74">&#39;Bob&#39;</span>, <span style="color:#e6db74">&#39;Charlie&#39;</span>, <span style="color:#e6db74">&#39;Dave&#39;</span>, <span style="color:#e6db74">&#39;Eva&#39;</span>],
304+
<span style="color:#e6db74">&#39;Age&#39;</span>: [<span style="color:#ae81ff">25</span>, <span style="color:#ae81ff">30</span>, <span style="color:#ae81ff">35</span>, <span style="color:#ae81ff">40</span>, <span style="color:#ae81ff">45</span>],
305+
<span style="color:#e6db74">&#39;City&#39;</span>: [<span style="color:#e6db74">&#39;New York&#39;</span>, <span style="color:#e6db74">&#39;Los Angeles&#39;</span>, <span style="color:#e6db74">&#39;Chicago&#39;</span>, <span style="color:#e6db74">&#39;San Francisco&#39;</span>, <span style="color:#e6db74">&#39;Seattle&#39;</span>]
306+
})
307+
308+
<span style="color:#75715e"># Select rows where the &#39;City&#39; column contains the string &#39;New&#39; or &#39;San&#39;</span>
309+
result <span style="color:#f92672">=</span> df[df[<span style="color:#e6db74">&#39;City&#39;</span>]<span style="color:#f92672">.</span>str<span style="color:#f92672">.</span>contains(<span style="color:#e6db74">&#39;New|San&#39;</span>)]
310+
311+
<span style="color:#75715e"># Display the result</span>
312+
print(result)
313+
</code></pre></div><p>In this example, we use the str.contains() method to select rows where the &lsquo;City&rsquo; column contains the string &lsquo;New&rsquo; or &lsquo;San&rsquo;. We pass the regular expression pattern &lsquo;New|San&rsquo; as an argument to the method to match either string. The resulting DataFrame is stored in the result variable and printed to the console.</p>
314+
<p>You can adjust the regular expression pattern to fit your specific needs and criteria. Note that the str.contains() method is case-sensitive by default, but you can set the case parameter to False to make it case-insensitive.</p>
315+
<h3 id="using-re-module">Using re module</h3>
316+
<p>You can also use the Python re module to select rows based on a regular expression pattern. Here&rsquo;s an example:</p>
317+
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> pandas <span style="color:#66d9ef">as</span> pd
318+
<span style="color:#f92672">import</span> re
319+
320+
<span style="color:#75715e"># Create a sample DataFrame</span>
321+
df <span style="color:#f92672">=</span> pd<span style="color:#f92672">.</span>DataFrame({
322+
<span style="color:#e6db74">&#39;Name&#39;</span>: [<span style="color:#e6db74">&#39;Alice&#39;</span>, <span style="color:#e6db74">&#39;Bob&#39;</span>, <span style="color:#e6db74">&#39;Charlie&#39;</span>, <span style="color:#e6db74">&#39;Dave&#39;</span>, <span style="color:#e6db74">&#39;Eva&#39;</span>],
323+
<span style="color:#e6db74">&#39;Age&#39;</span>: [<span style="color:#ae81ff">25</span>, <span style="color:#ae81ff">30</span>, <span style="color:#ae81ff">35</span>, <span style="color:#ae81ff">40</span>, <span style="color:#ae81ff">45</span>],
324+
<span style="color:#e6db74">&#39;City&#39;</span>: [<span style="color:#e6db74">&#39;New York&#39;</span>, <span style="color:#e6db74">&#39;Los Angeles&#39;</span>, <span style="color:#e6db74">&#39;Chicago&#39;</span>, <span style="color:#e6db74">&#39;San Francisco&#39;</span>, <span style="color:#e6db74">&#39;Seattle&#39;</span>]
325+
})
326+
327+
<span style="color:#75715e"># Select rows where the &#39;City&#39; column contains the string &#39;New&#39; or &#39;San&#39;</span>
328+
regex_pattern <span style="color:#f92672">=</span> re<span style="color:#f92672">.</span>compile(<span style="color:#e6db74">&#39;New|San&#39;</span>)
329+
result <span style="color:#f92672">=</span> df[df[<span style="color:#e6db74">&#39;City&#39;</span>]<span style="color:#f92672">.</span>apply(<span style="color:#66d9ef">lambda</span> x: bool(regex_pattern<span style="color:#f92672">.</span>search(x)))]
330+
331+
<span style="color:#75715e"># Display the result</span>
332+
print(result)
333+
</code></pre></div><p>In this example, we first import the re module to create a regular expression pattern to match strings containing either &lsquo;New&rsquo; or &lsquo;San&rsquo;. We then use the apply() method to apply the regular expression pattern to each value in the &lsquo;City&rsquo; column of the DataFrame. We use the search() method of the regular expression pattern to search for a match in each value.</p>
334+
<p>Finally, we use the bool() function to convert the search result to a Boolean value (i.e., True if there&rsquo;s a match and False otherwise). The resulting Boolean values are used to select only the rows where there is a match in the &lsquo;City&rsquo; column. The resulting DataFrame is stored in the result variable and printed to the console.</p>
335+
<p>Note that using the apply() method can be slower than using the str.contains() method directly, especially for large DataFrames. Therefore, it&rsquo;s recommended to use the str.contains() method whenever possible.</p>
271336

272337

273338
<link href="//cdn-images.mailchimp.com/embedcode/classic-10_7_dtp.css" rel="stylesheet" type="text/css">

tags/pandas/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<pubDate>Thu, 02 Mar 2023 20:17:18 +0900</pubDate>
1414

1515
<guid>https://thats-it-code.com/pandas/pandas__select-rows-from-a-dataframe-based-on-column-values/</guid>
16-
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method.&lt;/p&gt;
16+
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module.&lt;/p&gt;
1717
&lt;p&gt;&lt;img src=&#34;https://thats-it-code.com/img/pandas_select-rows.png&#34; alt=&#34;Pandas&#34;&gt;&lt;/p&gt;</description>
1818
</item>
1919

tags/tips-and-tricks/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<pubDate>Thu, 02 Mar 2023 20:17:18 +0900</pubDate>
1414

1515
<guid>https://thats-it-code.com/pandas/pandas__select-rows-from-a-dataframe-based-on-column-values/</guid>
16-
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method.&lt;/p&gt;
16+
<description>&lt;p&gt;In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query method, and methods using regular expression like str.match(), str.contains() and python re module.&lt;/p&gt;
1717
&lt;p&gt;&lt;img src=&#34;https://thats-it-code.com/img/pandas_select-rows.png&#34; alt=&#34;Pandas&#34;&gt;&lt;/p&gt;</description>
1818
</item>
1919

0 commit comments

Comments
 (0)