• Latest
  • Trending
How to Filter Data with Python

How to Filter Data with Python

March 1, 2022
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022
8 Most Common Causes of a Data Breach

5.7bn data entries found exposed on Chinese VPN

August 18, 2022
Fibre optic interconnection linking Cameroon and Congo now operational

Fibre optic interconnection linking Cameroon and Congo now operational

July 15, 2022
Ericsson and MTN Rwandacell Discuss their Long-Term Partnership

Ericsson and MTN Rwandacell Discuss their Long-Term Partnership

July 15, 2022
Airtel Africa Purchases $42M Worth of Additional Spectrum

Airtel Africa Purchases $42M Worth of Additional Spectrum

July 15, 2022
Huawei steps up drive for Kenyan talent

Huawei steps up drive for Kenyan talent

July 15, 2022
TSMC predicts Q3 revenue boost thanks to increased iPhone 13 demand

TSMC predicts Q3 revenue boost thanks to increased iPhone 13 demand

July 15, 2022
Facebook to allow up to five profiles tied to one account

Facebook to allow up to five profiles tied to one account

July 15, 2022
Top 10 apps built and managed in Ghana

Top 10 apps built and managed in Ghana

July 15, 2022
MTN Group to Host the 2nd Edition of the MoMo API Hackathon

MTN Group to Host the 2nd Edition of the MoMo API Hackathon

July 15, 2022
KIOXIA Introduce JEDEC XFM Removable Storage with PCIe/NVMe Spec

KIOXIA Introduce JEDEC XFM Removable Storage with PCIe/NVMe Spec

July 15, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Sunday, 5 February, 2023
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

How to Filter Data with Python

by ITECHNEWS
March 1, 2022
in Data Science, Leading Stories
0 0
0
How to Filter Data with Python

While data scientists can and do utilize SQL, it can quite frankly be easier to manipulate your pandas dataframe with Python operations instead (or, in addition to). I, personally, like to have a mix of both languages to structure my data. At a certain point, it can be more efficient to work with operations once you have an already queried dataframe from SQL. For example, you might query all your necessary columns, and then read in your dataframe, then apply the respective operations to organize your data before it will ultimately be ingested into your data science model. With that being said, let’s dive a little deeper into some simple operations that might make your everyday work a little easier.

 

YOU MAY ALSO LIKE

Inaugural AfCFTA Conference on Women and Youth in Trade

Instagram fined €405m over children’s data privacy

Lesser/Greater

For all these use cases, I will have a pretend pandas dataframe.

This following operation is “lesser than”, so you can write your dataframe alias, which in this case, is just df. You can insert the column name where I have placed ‘column_1’. I have assigned a new dataframe, named df_less_than_20, so that I only have records/rows that are the column value that is less than 20.

df_less_than_20 = df[df['column_1'] < 20]

The same concept can be applied to greater than:

df_more_than_20 = df[df['column_1'] > 20]

Although these operations are simple, they are still useful, and, when put together, can be even more beneficial — as we will see below.

Another way to look at this feature is like the WHERE clause in SQL.

 

And/Or

Now that we have the above statement, we can apply a further filter to our data.

We can use both, or either the & or | operation.

To clarify:

  • AND = &
  • OR = |

I was aware of the AND operation, but the OR was actually a recent operation that I found that has been incredibly useful, especially when filtering out data for accuracy and error analysis after your model is run. Of course, you can use this operation before that step of the process as well.

Now, we can use either or both of these in the following way:

df[(df['column_1'] >= -100) & (df['column_1'] <= 1000)]

The above is saying, give me the data where the value is between negative 100 and positive 100.

A next step, is to use the OR operation, to find all rows that are negative as well:

df[(df['column_1'] < 0) | (df['column_1'] >= -100) & (df['column_1'] <= 100)]

We can also strip away the middle clause to create the following snippet:

df[(df['column_1'] < 0) | (df['column_1'] <= 100)]

However, we could replace one of the clauses with something that is filtering on another column with another value as well.

df[(df['column_1'] < 0) | (df['column_2'] <= 50)]

Does/Does not Equal

Lastly, we have another way to filter our data by selecting rows where there is a certain value or there is not a certain value.

These two operations look like the following

  • DOES EQUAL: ==
  • DOES NOT EQUAL: !=

Here are a few examples of both:

df[df['column_1'] == 100]
df[df['column_2'] == 50]
df[df['column_3'] == 'blue']
df[df['column_3'] != 'blue']
df[(df['column_3'] != 'red' ) | (df['column_200'] <= 8.60)]

 

Summary

To summarize, we saw that we could combine a few of the operations that we discussed above to create a filtered dataset or pandas dataframe. Ultimately, this type of coding might be easier for some data scientists, who prefer to work in Python rather than in SQL.

Source: Matthew Przybyla, KDnuggets
Tags: How to Filter Data with Python
ShareTweetShare
Plugin Install : Subscribe Push Notification need OneSignal plugin to be installed.

Search

No Result
View All Result

Recent News

Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022
8 Most Common Causes of a Data Breach

5.7bn data entries found exposed on Chinese VPN

August 18, 2022

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022

Recent News

  • Inaugural AfCFTA Conference on Women and Youth in Trade September 6, 2022
  • Instagram fined €405m over children’s data privacy September 6, 2022
  • 5.7bn data entries found exposed on Chinese VPN August 18, 2022
  • Fibre optic interconnection linking Cameroon and Congo now operational July 15, 2022
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© 2021-2022 iTechNewsOnline.Com - Powered by BackUPDataSystems

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© 2021-2022 iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Go to mobile version