Karim Aya

Karim Aya

1568357900

Understand and use array methods

There is no better way to learn than to take things apart and put them back together. Note that when working on our implementations, we won’t be overriding existing methods, since that is never a good idea (some packages we import may be dependent on it). Also, this is going to allow us to compare how our versions fare against the original methods.

So instead of writing this:

Array.prototype.map = function map() {
 // implementation
};

We are going to do this:

function map(array) {
 // takes an array as the first argument
 // implementation
}

We could also implement our methods by using the class keyword and extending the Array constructor like so:

class OwnArray extends Array {
 public constructor(...args) {
   super(...args);
 }

public map() {
// implementation
return this;
}
}

The only difference would be that instead of using the array argument, we would be using the this keyword.

However, I felt this would bring about unnecessary confusion, so we are going to stick with the first approach.

With that out of the way, let’s kick it off by implementing the easiest one — the forEach method!

Iterating over collections

.forEach

The Array.prototype.forEach method takes a callback function and executes it for each item in the array without mutating the array in any way.

[1, 2, 3, 4, 5].forEach(value => console.log(value));

Implementation

function forEach(array, callback) {
const { length } = array;

for (let index = 0; index < length; index += 1) {
const value = array[index];
callback(value, index, array);
}
}

We iterate over the array and execute the callback for each element. The important thing to note here is that the method doesn’t return anything — so, in a way, it returns undefined.

Method chaining

What’s great about working with array methods is the possibility of chaining operations together. Consider the following code:

function getTodosWithCategory(todos, category) {
return todos
.filter(todo => todo.category === category)
.map(todo => normalizeTodo(todo));
}

This way, we don’t have to save the result of map to a variable and generally have better-looking code as a result.

Unfortunately, forEach doesn’t return the input array! This means we can’t to do the following:

// Won’t work!
function getTodosWithCategory(todos, category) {
return todos
.filter(todo => todo.category === category)
.forEach((value) => console.log(value))
.map(todo => normalizeTodo(todo));
}

The console.log here, of course, is useless.

A logging utility function

I have written a simple utility function that will better explain what each method does: what it takes as input, what it returns, and whether or not it mutates the array.

function logOperation(operationName, array, callback) {
const input = […array];
const result = callback(array);

console.log({
operation: operationName,
arrayBefore: input,
arrayAfter: array,
mutates: mutatesArray(input, array), // shallow check
result,
});
}

Here’s the utility function run for our implementation of forEach:

logOperation(‘forEach’, [1, 2, 3, 4, 5], array => forEach(array, value => console.log(value)));
{
operation: ‘forEach’,
arrayBefore: [ 1, 2, 3, 4, 5 ],
arrayAfter: [ 1, 2, 3, 4, 5 ],
mutates: false,
result: undefined
}

Due to the fact that we implement the methods as functions, we have to use the following syntax: forEach(array, …) instead of array.forEach(…).

Note: I have also created test cases for every method to be sure they work as expected — you can find them in the repository.

.map

One of the most commonly used methods is Array.prototype.map. It lets us create a new array by converting the existing values into new ones.

[1, 2, 3].map(number => number * 5);
// -> [5, 10, 15]

Implementation

function map(array, callback) {
const result = [];
const { length } = array;

for (let index = 0; index < length; index += 1) {
const value = array[index];

result[index] = callback(value, index, array);
}

return result;
}

The callback provided to the method takes the old value as an argument and returns a new value, which is then saved under the same index in the new array, here called result.

It is important to note here that we return a new array; we don’t modify the old one. This is an important distinction to make due to arrays and objects being passed as references here. If you are confused by the whole references versus values thing, here’s a great read.

logOperation(‘map’, [1, 2, 3, 4, 5], array => map(array, value => value + 5));
{
operation: ‘map’,
input: [ 1, 2, 3, 4, 5 ],
output: [ 6, 7, 8, 9, 10 ],
mutates: false
}

.filter

Another very useful method is Array.prototype.filter. As the name suggests, it filters out the values for which the callback returned is false. Each value is saved in a new array that is later returned.

[1, 2, 3, 4, 5].filter(number => number >= 3);
// -> [3, 4, 5]

Implementation

function filter(array, callback) {
const result = [];

const { length } = array;

for (let index = 0; index < length; index += 1) {
const value = array[index];

if (callback(value, index, array)) {
push(result, value);
}
}

return result;
}

We take each value and check whether the provided callback has returned true or false and either append the value to the newly created array or discard it, appropriately.

Note that here we use the push method on the result array instead of saving the value at the same index it was placed in the input array. This way, result won’t have empty slots because of the discarded values.

logOperation(‘filter’, [1, 2, 3, 4, 5], array => filter(array, value => value >= 2));
{
operation: ‘filter’,
input: [ 1, 2, 3, 4, 5 ],
output: [ 2, 3, 4, 5 ],
mutates: false
}

.reduce

The reduce method is, admittedly, one of the more complicated methods. The extensiveness of its use, however, cannot be overstated, and so it is crucial to get a good grasp on how it works. It takes an array and spits out a single value. In a sense, it reduces the array down to that very value.

How that value is computed, exactly, is what needs to be specified in the callback. Let’s consider an example — the simplest use of reduce, i.e., summing an array of numbers:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reduce((sum, number) => {
return sum + number;
}, 0) // -> 55

Note how the callback here takes two arguments: sum and number. The first one is always the result returned by the previous iteration, and the second one is the element of the array we’re currently considering in the loop.

And so here, as we iterate over the array, sum is going to contain the sum of numbers up to the current index of the loop since with each iteration we just add to it the current value of the array.

Implementation

function reduce(array, callback, initValue) {
const { length } = array;

let acc = initValue;
let startAtIndex = 0;

if (initValue === undefined) {
acc = array[0];
startAtIndex = 1;
}

for (let index = startAtIndex; index < length; index += 1) {
const value = array[index];
acc = callback(acc, value, index, array);
}

return acc;
}

We create two variables, acc and startAtIndex, and initialize them with their default values, which are the argument initValue and 0, respectively.

Then, we check whether or not initValue is undefined. If it is, we have to set as the initial value the first value of the array and, so as not to count the initial element twice, set the startAtIndex to 1.

Each iteration, the reduce method saves the result of the callback in the accumulator (acc), which is then available in the next iteration. For the first iteration, the accumulator is set to either the initValue or array[0].

Searching

What operation on arrays can be more common than searching for some specific value? Here are a few methods to help us with this.

.findIndex

As the name suggests, findIndex helps us find the index of a given value inside the array.

[1, 2, 3, 4, 5, 6, 7].findIndex(value => value === 5); // 4

The method executes the provided callback for each item in the array until the callback returns true. The method then returns the current index. Should no value be found, -1 is returned.

Implementation

function findIndex(array, callback) {
const { length } = array;

for (let index = 0; index < length; index += 1) {
const value = array[index];

if (callback(value, index, array)) {
return index;
}
}

return -1;
}

.find

find only differs from findIndex in that it returns the actual value instead of its index. In our implementation, we can reuse the already-implemented findIndex.

[1, 2, 3, 4, 5, 6, 7].findIndex(value => value === 5); // 5

Implementation

function find(array, callback) {
const index = findIndex(array, callback);

if (index === -1) {
return undefined;
}

return array[index];
}

.indexOf

indexOf is another method for getting an index of a given value. This time, however, we pass the actual value as an argument instead of a function. Again, to simplify the implementation, we can use the previously implemented findIndex!

[3, 2, 3].indexOf(3); // -> 0

Implementation

function indexOf(array, searchedValue) {
return findIndex(array, value => value === searchedValue);
}

We provide an appropriate callback to findIndex, based on the value we are searching for.

.lastIndexOf

lastIndexOf works the same way as indexOf, only it starts at the end of an array. We also (like indexOf) pass the value we are looking for as an argument instead of a callback.

[3, 2, 3].lastIndexOf(3); // -> 2

Implementation

function lastIndexOf(array, searchedValue) {
for (let index = array.length - 1; index > -1; index -= 1) {
const value = array[index];

if (value === searchedValue) {
return index;
}
}

return -1;
}

We do the same thing we did for findIndex, but instead of executing a callback, we compare value and searchedValue. Should the comparison yield true, we return the index; if we

don’t find the value, we return -1.

.every

The every method comes in handy when we want to check whether all elements of an array satisfy a given condition.

[1, 2, 3].every(value => Number.isInteger(value)); // -> true

You can think of the every method as an array equivalent of the logical AND.

Implementation

function every(array, callback) {
const { length } = array;

for (let index = 0; index < length; index += 1) {
const value = array[index];

if (!callback(value, index, array)) {
return false;
}
}

return true;
}

We execute the callback for each value. If false is returned at any point, we exit the loop and the whole method returns false. If the loop terminates without setting off the if statement (all elements yield true), the method returns true.

.some

And now for the complete opposite of everysome. Even if only one execution of the callback returns true, the function returns true. Analogically to the every method, you can think of the some method as an array equivalent of the logical OR.

[1, 2, 3, 4, 5].some(number => number === 5); // -> true

Implementation

function some(array, callback) {
const { length } = array;

for (let index = 0; index < length; index += 1) {
const value = array[index];

if (callback(value, index, array)) {
return true;
}
}

return false;
}

We execute the callback for each value. If true is returned at any point we exit the loop and the whole method returns true. If the loop terminates without setting off the if statement (all elements yield false), the method returns false.

.includes

The includes method works like the some method, but instead of a callback, we provide as an argument a value to compare elements to.

[1, 2, 3].includes(3); // -> true

Implementation

function includes(array, searchedValue) {
return some(array, value => value === searchedValue);
}

Flattening

Sometimes our arrays become two or three levels deep and we would like to flatten them, i.e., reduce the degree to which they are nested. For example, say we’d like to bring all values to the top level. To our aid come two new additions to the language: the flat and flatMap methods.

.flat

The flat method reduces the depth of the nesting by pulling the values out of the nested array.

[1, 2, 3, [4, 5, [6, 7, [8]]]].flat(1); // -> [1, 2, 3, 4, 5, [6, 7, [8]]]

Since the level we provided as an argument is 1, only the first level of arrays is flattened; the rest stay the same.

[1, 2, 3, [4, 5]].flat(1) // -> [1, 2, 3, 4, 5]

Implementation

function flat(array, depth = 0) {
if (depth < 1 || !Array.isArray(array)) {
return array;
}

return reduce(
array,
(result, current) => {
return concat(result, flat(current, depth - 1));
},
[],
);
}

First, we check whether or not the depth argument is lower than 1. If it is, it means there is nothing to flatten, and we should simply return the array.

Second, we check whether the array argument is actually of the type Array, because if it isn’t, then the notion of flattening is meaningless, so we simply return this argument instead.

We make use of the reduce function, which we have implemented before. We start with an empty array and then take each value of the array and flatten it.

Note that we call the flat function with (depth - 1). With each call, we decrement the depth argument as to not cause an infinite loop. Once the flattening is done, we append the returned value to the result array.

Note: the concat function is used here to merge two arrays together. The implementation of the function is explained below.

.flatMap

flatMap, as the name might suggest, is a combination of flat and map. First we map according to the callback and later flatten the result.

In the map method above, for each value, we returned precisely one value. This way, an array with three items still had three items after the mapping. With flatMap, inside the provided callback we can return an array, which is later flattened.

[1, 2, 3].flatMap(value => [value, value, value]); // [1, 1, 1, 2, 2, 2, 3, 3, 3]

Each returned array gets flattened, and instead of getting an array with three arrays nested inside, we get one array with nine items.

Implementation

function flatMap(array, callback) {
return flat(map(array, callback), 1);
}

As per the explanation above, we first use map and then flatten the resulting array of arrays by one level.

Joining, appending, and reversing arrays

.concat

As you’ve just seen, the concat method is very useful for merging two or more arrays together. It is widely used because it doesn’t mutate the arrays; instead, it returns a new one that all the provided arrays are merged into.

[1, 2, 3].concat([4, 5], 6, [7, 8]) // -> [1, 2, 3, 4, 5, 6, 7, 8]

Implementation

function concat(array, …values) {
const result = […array];
const { length } = values;

for (let index = 0; index < length; index += 1) {
const value = values[index];

if (Array.isArray(value)) {
push(result, …value);
} else {
push(result, value);
}
}

return result;
}

concat takes an array as the first argument and an unspecified number of values that could be arrays (but also could be anything else — say, primitive values) as the second argument.

At first, we create the result array by copying the provided array (using the spread operator, which spreads the provided array’s values into a new array). Then, as we iterate over the rest of the values provided, we check whether the value is an array or not. If it is, we use the push function to append its values to the result array.

If we did push(result, value), we would only append the array as one element. Instead, by using the spread operator push(result, …value), we are appending all the values of the array to the result array. In a way, we flatten the array one level deep!

Otherwise, if the current value is not an array, we also push the value to the result array — this time, of course, without the spread operator.

.join

The join method turns an array into a string, separating the values with a string of choice.

[‘Brian’, ‘Matt’, ‘Kate’].join(', ') // -> Brian, Matt, Kate

Implementation

function join(array, joinWith) {
return reduce(
array,
(result, current, index) => {
if (index === 0) {
return current;
}

 return `${result}${joinWith}${current}`;

},
‘’,
);
}

We make use of the reduce function: we pass to it the provided array and set the initial value to an empty string. Pretty straightforward so far.

The callback of reduce is where the magic happens: reduce iterates over the provided array and pieces together the resulting string, placing the desired separator (passed as joinWith) in between the values of the array.

The array[0] value requires some special treatment, since at that point result is still undefined (it’s an empty string), and we don’t want the separator (joinWith) in front of the first element, either.

.reverse

The reverse method reverses the order of values in an array.

[1, 2, 3].reverse(); // -> [3, 2, 1]

Implementation

function reverse(array) {
const result = [];

const lastIndex = array.length - 1;

for (let index = lastIndex; index > -1; index -= 1) {
const value = array[index];
result[lastIndex - index] = value;
}

return result;
}

The idea is simple: first, we define an empty array and save the last index of the one provided as an argument. We iterate over the provided array in reverse, saving each value at (lastIndex - index) place in the result array, which we return afterwards.

Adding, removing, and appending values

.shift

The shift method shifts the values of an array down by one index and by doing so removes the first value, which is then returned.

[1, 2, 3].shift(); // -> 1

Implementation

function shift(array) {
const { length } = array;
const firstValue = array[0];

for (let index = 1; index < length; index += 1) {
const value = array[index];
array[index - 1] = value;
}

array.length = length - 1;

return firstValue;
}

We start by saving the provided array’s original length and its initial value (the one we’ll drop when we shift everything by one). We then iterate over the array and move each value down by one index. Once done, we update the length of the array and return the once-initial value.

.unshift

The unshift method adds one or more values to the beginning of an array and returns that array’s length.

[2, 3, 4].unshift(1); // -> [1, 2, 3, 4]

Implementation

function unshift(array, …values) {
const mergedArrays = concat(values, …array);
const { length: mergedArraysLength } = mergedArrays;

for (let index = 0; index < mergedArraysLength; index += 1) {
const value = mergedArrays[index];
array[index] = value;
}

return array.length;
}

We start by concatenating values (individual values passed as arguments) and array (the array we want to unshift). It is important to note here that values come first; they are to be placed in front of the original array.

We then save the length of this new array and iterate over it, saving its values in the original array and overwriting what was there to begin with.

.slice

Taking out a single value out of an array is simple: we just refer to it using its index. Sometimes, however, we would like to take a bigger slice of an array — say, three or four elements at once. That’s when the slice method comes in handy.

We specify the start and the end indices, and slice hands us the array cut from the original array at these indices. Note, however, that the end index argument is not inclusive; in the following example, only elements of indices 34, and 5 make it to the resulting array.

[1, 2, 3, 4, 5, 6, 7].slice(3, 6); // -> [4, 5, 6]

Implementation

function slice(array, startIndex = 0, endIndex = array.length) {
const result = [];

for (let index = startIndex; index < endIndex; index += 1) {
const value = array[index];

if (index < array.length) {
push(result, value);
}
}

return result;
}

We iterate over the array from startIndex to endIndex and push each value to the result. We also make use of the default parameters here so that the slice method simply creates a copy of the array when no arguments are passed. We achieve this by setting by default startIndex to 0 and endIndex to the array’s length.

Note: the if statement makes sure we push only if the value under a given index exists in the original array.

.splice

The splice method simultaneously removes a given number of values from the array and inserts in their place some other values. Although not obvious at first, we can add more values than we remove and vice versa.

First, we specify the starting index, then how many values we would like to remove, and the rest of the arguments are the values to be inserted.

const arr = [1, 2, 3, 4, 5];

arr.splice(0, 2, 3, 4, 5);

arr // -> [3, 4, 5, 3, 4, 5]

Implementation

function splice(array, insertAtIndex, removeNumberOfElements, …values) {
const firstPart = slice(array, 0, insertAtIndex);
const secondPart = slice(array, insertAtIndex + removeNumberOfElements);

const removedElements = slice(array, insertAtIndex, insertAtIndex + removeNumberOfElements);

const joinedParts = firstPart.concat(values, secondPart);
const { length: joinedPartsLength } = joinedParts;

for (let index = 0; index < joinedPartsLength; index += 1) {
array[index] = joinedParts[index];
}

return removedElements;
}

The idea is to make two cuts at insertAtIndex and insertAtIndex + removeNumberOfElements. This way, we slice the original array into three pieces. The first piece (firstPart) as well as the third one (here called secondPart) are what will make it into the resulting array.

It is between these two that we will insert the values we passed as arguments. We do this with the concat method. The remaining middle part is removedElements, which we return in the end.

.pop

The pop method removes the last value of an array and returns it.

[1, 2, 3].pop(); // -> 3

Implementation

function pop(array) {
const value = array[array.length - 1];

array.length = array.length - 1;

return value;
}

First, we save the last value of the array in a variable. Then we simply reduce the array’s length by one, removing the last value as a result.

.push

The push method lets us append values at the end of an array.

[1, 2, 3, 4].push(5); // -> [1, 2, 3, 4, 5]

Implementation

export function push(array, …values) {
const { length: arrayLength } = array;
const { length: valuesLength } = values;

for (let index = 0; index < valuesLength; index += 1) {
array[arrayLength + index] = values[index];
}

return array.length;
}

First we save the length of the original array and how many values to append there are in their respective variables. We then iterate over the provided values and append them to the original array.

We start the loop at index = 0, so each iteration we add to index the array’s length. This way we don’t overwrite any values in the original array, but actually append them.

.fill

The fill method is of use when we want to fill an empty array with, say, a placeholder value. If we wanted to create an array with a specified number of null elements, we could do it like this:

[…Array(5)].fill(null) // -> [null, null, null, null, null]

Implementation

function fill(array, value, startIndex = 0, endIndex = array.length) {
for (let index = startIndex; index < endIndex; index += 1) {
array[index] = value;
}

return array;
}

All the fill method really does is replace an array’s values in the specified range of indexes. If the range is not provided, the method replaces all the array’s values.

With generators

The last three methods are special in the way that they return generators. If you are not familiar with generators, feel free to skip them, as you likely won’t use them anytime soon.

.values

The values method returns a generator that yields values of an array.

const valuesGenerator = values([1, 2, 3, 4, 5]);

valuesGenerator.next(); // { value: 1, done: false }

Implementation

function values(array) {
const { length } = array;

function* createGenerator() {
for (let index = 0; index < length; index += 1) {
const value = array[index];
yield value;
}
}

return createGenerator();
}

First, we define the createGenerator function. In it, we iterate over the array and yield each value.

.keys

The keys method returns a generator that yields indices of an array.

const keysGenerator = keys([1, 2, 3, 4, 5]);

keysGenerator.next(); // { value: 0, done: false }

Implementation

function keys(array) {
function* createGenerator() {
const { length } = array;

for (let index = 0; index < length; index += 1) {
yield index;
}
}

return createGenerator();
}

The implementation is exactly the same, but this time, we yield an index, not a value.

.entries

The entries method returns a generator that yields index-value pairs.

const entriesGenerator = entries([1, 2, 3, 4, 5]);

entriesGenerator.next(); // { value: [0, 1], done: false }

Implementation

function entries(array) {
const { length } = array;

function* createGenerator() {
for (let index = 0; index < length; index += 1) {
const value = array[index];
yield [index, value];
}
}

return createGenerator();
}

Again, the same implementation, but now we combine both the index and the value and yield them in an array.

Summary

Using the array’s methods efficiently is the basis for becoming a good developer. Acquainting yourself with the intricacies of their inner workings is the best way I know to get good at it.

Note: I didn’t cover sort and toLocaleString here because their implementations are overly complicated and, for my taste, too convoluted for beginners. I also didn’t discuss copyWithin, since it’s never used — it’s absolutely useless.

Originally published by Maciej Cieślar at  blog.logrocket.com

===========================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

New ES2019 Features Every JavaScript Developer Should Know

Learn JavaScript: Full-Stack from Scratch

Understanding the Spread Operator in JavaScript

JavaScript Basics Before You Learn React


#javascript #web-development

What is GEEK

Buddha Community

Understand and use array methods

Why Use WordPress? What Can You Do With WordPress?

Can you use WordPress for anything other than blogging? To your surprise, yes. WordPress is more than just a blogging tool, and it has helped thousands of websites and web applications to thrive. The use of WordPress powers around 40% of online projects, and today in our blog, we would visit some amazing uses of WordPress other than blogging.
What Is The Use Of WordPress?

WordPress is the most popular website platform in the world. It is the first choice of businesses that want to set a feature-rich and dynamic Content Management System. So, if you ask what WordPress is used for, the answer is – everything. It is a super-flexible, feature-rich and secure platform that offers everything to build unique websites and applications. Let’s start knowing them:

1. Multiple Websites Under A Single Installation
WordPress Multisite allows you to develop multiple sites from a single WordPress installation. You can download WordPress and start building websites you want to launch under a single server. Literally speaking, you can handle hundreds of sites from one single dashboard, which now needs applause.
It is a highly efficient platform that allows you to easily run several websites under the same login credentials. One of the best things about WordPress is the themes it has to offer. You can simply download them and plugin for various sites and save space on sites without losing their speed.

2. WordPress Social Network
WordPress can be used for high-end projects such as Social Media Network. If you don’t have the money and patience to hire a coder and invest months in building a feature-rich social media site, go for WordPress. It is one of the most amazing uses of WordPress. Its stunning CMS is unbeatable. And you can build sites as good as Facebook or Reddit etc. It can just make the process a lot easier.
To set up a social media network, you would have to download a WordPress Plugin called BuddyPress. It would allow you to connect a community page with ease and would provide all the necessary features of a community or social media. It has direct messaging, activity stream, user groups, extended profiles, and so much more. You just have to download and configure it.
If BuddyPress doesn’t meet all your needs, don’t give up on your dreams. You can try out WP Symposium or PeepSo. There are also several themes you can use to build a social network.

3. Create A Forum For Your Brand’s Community
Communities are very important for your business. They help you stay in constant connection with your users and consumers. And allow you to turn them into a loyal customer base. Meanwhile, there are many good technologies that can be used for building a community page – the good old WordPress is still the best.
It is the best community development technology. If you want to build your online community, you need to consider all the amazing features you get with WordPress. Plugins such as BB Press is an open-source, template-driven PHP/ MySQL forum software. It is very simple and doesn’t hamper the experience of the website.
Other tools such as wpFoRo and Asgaros Forum are equally good for creating a community blog. They are lightweight tools that are easy to manage and integrate with your WordPress site easily. However, there is only one tiny problem; you need to have some technical knowledge to build a WordPress Community blog page.

4. Shortcodes
Since we gave you a problem in the previous section, we would also give you a perfect solution for it. You might not know to code, but you have shortcodes. Shortcodes help you execute functions without having to code. It is an easy way to build an amazing website, add new features, customize plugins easily. They are short lines of code, and rather than memorizing multiple lines; you can have zero technical knowledge and start building a feature-rich website or application.
There are also plugins like Shortcoder, Shortcodes Ultimate, and the Basics available on WordPress that can be used, and you would not even have to remember the shortcodes.

5. Build Online Stores
If you still think about why to use WordPress, use it to build an online store. You can start selling your goods online and start selling. It is an affordable technology that helps you build a feature-rich eCommerce store with WordPress.
WooCommerce is an extension of WordPress and is one of the most used eCommerce solutions. WooCommerce holds a 28% share of the global market and is one of the best ways to set up an online store. It allows you to build user-friendly and professional online stores and has thousands of free and paid extensions. Moreover as an open-source platform, and you don’t have to pay for the license.
Apart from WooCommerce, there are Easy Digital Downloads, iThemes Exchange, Shopify eCommerce plugin, and so much more available.

6. Security Features
WordPress takes security very seriously. It offers tons of external solutions that help you in safeguarding your WordPress site. While there is no way to ensure 100% security, it provides regular updates with security patches and provides several plugins to help with backups, two-factor authorization, and more.
By choosing hosting providers like WP Engine, you can improve the security of the website. It helps in threat detection, manage patching and updates, and internal security audits for the customers, and so much more.

Read More

#use of wordpress #use wordpress for business website #use wordpress for website #what is use of wordpress #why use wordpress #why use wordpress to build a website

Brook  Legros

Brook Legros

1659199883

String Pattern: Generate Strings Supplying A Simple Pattern in Ruby

StringPattern

With this gem, you can easily generate strings supplying a very simple pattern. Even generate random words in English or Spanish. Also, you can validate if a text fulfills a specific pattern or even generate a string following a pattern and returning the wrong length, value... for testing your applications. Perfect to be used in test data factories.

Also you can use regular expressions (Regexp) to generate strings: /[a-z0-9]{2,5}\w+/.gen

To do even more take a look at nice_hash gem

Installation

Add this line to your application's Gemfile:

gem 'string_pattern'

And then execute:

$ bundle

Or install it yourself as:

$ gem install string_pattern

Usage

What is a string pattern?

A pattern is a string where we supply these elements "a-b:c" where a is min_length, b is max_length (optional) and c is a set of symbol_type

min_length: minimum length of the string

max_length (optional): maximum length of the string. If not provided, the result will be with the min_length provided

symbol_type: The type of the string we want.
    x: from a to z (lowercase)
    X: A to Z (capital letters)
    L: A to Z and a to z
    T: National characters defined on StringPattern.national_chars
    n or N: for numbers. 0 to 9
    $: special characters, $%&#...  (includes blank space)
    _: blank space
    *: all characters
    0: empty string will be accepted.  It needs to be at the beginning of the symbol_type string
        @: It will generate a valid email following the official algorithm. It cannot be used with other symbol_type
        W: for English words, capital and lower. It cannot be used with other symbol_type
        w: for English words only lower and words separated by underscore. It cannot be used with other symbol_type
        P: for Spanish words, capital and lower. It cannot be used with other symbol_type
        p: for Spanish words only lower and words separated by underscore. It cannot be used with other symbol_type
    

How to generate a string following a pattern

To generate a string following a pattern you can do it using directly the StringPattern class or the generate method in the class, be aware you can always use also the alias method: gen

require 'string_pattern'

#StringPattern class
p StringPattern.generate "10:N"
#>3448910834
p StringPattern.gen "5:X"
#>JDDDK

#String class
p "4:Nx".gen
#>xaa3

#Symbol class
p :"10:T".generate
#>AccBdjklñD

#Array class
p [:"3:N", "fixed", :"3:N"].gen
#>334fixed920
p "(,3:N,) ,3:N,-,2:N,-,2:N".split(',').generate 
#>(937) 980-65-05

#Kernel
p gen "3:N"
#>443

Generating unique strings

If you want to generate for example 1000 strings and be sure all those strings are different you can use:

StringPattern.dont_repeat = true #default: false
1000.times {
    puts :"6-20:L/N/".gen
}
StringPattern.cache_values = Hash.new() #to clean the generated values from memory

Using dont_repeat all the generated string during the current run will be unique.

In case you just want one particular string to be unique but not the rest then add to the pattern just in the end the symbol: &

The pattern needs to be a symbol object.

1000.times {
    puts :"6-20:L/N/&".gen #will be unique
    puts :"10:N".gen
}

Generate words randomly in English or Spanish

To generate a string of the length you want that will include only real words, use the symbol types:

  • W: generates English words following CamelCase ('ExampleOutput')
  • w: generates English words following snake_case ('example_output')
  • P: generates Spanish words following CamelCase ('EjemploSalida')
  • p: generates Spanish words following snake_case ('ejemplo_salida')
require 'string_pattern'

puts '10-30:W'.gen
#> FirstLieutenant
puts '10-30:w'.gen
#> paris_university
puts '10-30:P'.gen
#> SillaMetalizada
puts '10-30:p'.gen
#> despacho_grande

If you want to use a different word separator than "_" when using 'w' or 'p':

# blank space for example
require 'string_pattern'

StringPattern.word_separator = ' '

puts '10-30:w'.gen
#> paris university
puts '10-30:p'.gen
#> despacho grande

The word list is loaded on the first request to generate words, after that the speed to generate words increases amazingly. 85000 English words and 250000 Spanish words. The vocabularies are a sample of public open sources.

Generate strings using Regular Expressions (Regexp)

Take in consideration this feature is not supporting all possibilities for Regular expressions but it is fully functional. If you find any bug or limitation please add it to issues: https://github.com/MarioRuiz/string_pattern/issues

In case you want to change the default maximum for repetitions when using * or +: StringPattern.default_infinite = 30 . By default is 10.

If you want to translate a regular expression into an StringPattern use the method we added to Regexp class: to_sp

Examples:

/[a-z0-9]{2-5}\w+/.to_sp
#> ["2-5:nx", "1-10:Ln_"]

#regular expression for UUID v4
/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/.to_sp
#> ["8:n[ABCDEF]", "-", "4:n[ABCDEF]", "-4", "3:n[ABCDEF]", "-", "1:[89AB]", "3:n[ABCDEF]", "-", "12:n[ABCDEF]"]

If you want to generate a random string following the regular expression, you can do it like a normal string pattern:


regexp = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/

# using StringPattern class
puts StringPattern.generate(regexp)

# using Kernel
puts generate(regexp)

# using generate method added to Regexp class
puts regexp.generate

#using the alias 'gen'
puts regexp.gen 

# output:
#>7009574B-6F2F-436E-BB7A-EA5FDA6B4E47
#>5FB1718F-108A-4F62-8170-33C43FD86B1D
#>05745B6F-93BA-475F-8118-DD56E5EAC4D1
#>2D6FC189-8D50-45A8-B182-780193838502

String patterns

How to generate one or another string

In case you need to specify that the string is generated selecting one or another fixed string or pattern, you can do it by using Array of patterns and in the position you want you can add an array with the possible values

p ["uno:", :"5:N", ['.red','.green', :'3:L'] ].gen

# first position a fixed string: "uno:"
# second position 5 random numbers
# third position one of these values: '.red', '.green' or 3 letters

# example output: 
# 'uno:34322.red'
# 'uno:44432.green'
# 'uno:34322.red'
# 'uno:28795xAB'

Take in consideration that this is only available to generate successful strings but not for validation

Custom characters

Also, it's possible to provide the characters we want. To do that we'll use the symbol_type [characters]

If we want to add the character ] we have to write ]]

Examples

# four chars from the ones provided: asDF9
p "4:[asDF9]".gen    #> aaaa, asFF, 9sFD

# from 2 to 20 chars, capital and lower chars (Xx) and also valid the characters $#6
p "2-20:[$#6]Xx".gen    #> aaaa, asFF, 66, B$DkKL#9aDD
 
# four chars from these: asDF]9
p "4:[asDF]]9]".gen    #> aa]a, asFF, 9s]D

Required characters or symbol types

We'll use the symbol / to specify which characters or symbols we want to be included on the resulting string as required values /symbols or characters/

If we need to add the character / we'll use //

Examples:

# four characters. optional: capitals and numbers, required: lower
"4:XN/x/".gen    # aaaa, FF9b, j4em, asdf, ADFt

# from 6 to 15 chars. optional: numbers, capitals and the chars $ and Æ. required the chars: 23abCD
"6-15:[/23abCD/$Æ]NX".gen    # bCa$D32, 32DJIOKLaCb, b23aD568C
 
# from 4 to 9 chars. optional: numbers and capitals. required: lowers and the characters $ and 5
"4-9:[/$5/]XN/x/".generate    # aa5$, F5$F9b, j$4em5, a5sdf$, $ADFt5 

Excluded characters

If we want to exclude a few characters in the result, we'll use the symbol %characters%

If you need to exclude the character %, you should use %%

Examples:

# from 2 to 20 characters. optional: Numbers and characters A, B and C. excluded: the characters 8 and 3
"2-20:[%83%ABC]N".gen    # B49, 22900, 9CAB, 22, 11CB6270C26C4572A50C

# 10 chars. optional: Letters (capital and lower). required: numbers. excluded: the characters 0 and WXYzZ
"10:L/n/[%0WXYzZ%]".gen    # GoO2ukCt4l, Q1Je2remFL, qPg1T92T2H, 4445556781

Not fulfilling a pattern

If we want our resulting string doesn't fulfill the pattern we supply, then we'll use the symbol ! at the beginning

Examples:

"!4:XN/x/".gen    # a$aaa, FF9B, j4DDDem, as, 2345

"!10:N".gen     # 123, 34899Add34, 3434234234234008, AAFj#kd2x

Generate a string with specific expected errors

Usually, for testing purposes you need to generate strings that don't fulfill a specific pattern, then you can supply as a parameter expected_errors (alias: errors)

The possible values you can specify is one or more of these ones: :length, :min_length, :max_length, :value, :required_data, :excluded_data, :string_set_not_allowed

:length: wrong length, minimum or maximum
:min_length: wrong minimum length
:max_length: wrong maximum length
:value: wrong resultant value
:required_data: the output string won't include all necessary required data. It works only if required data supplied on the pattern.
:excluded_data: the resultant string will include one or more characters that should be excluded. It works only if excluded data supplied on the pattern.
:string_set_not_allowed: it will include one or more characters that are not supposed to be on the string.

Examples:

"10-20:N".gen errors: [:min_length]
#> 627, 098262, 3408

"20:N".gen errors: [:length, :value]
#> |13, tS1b)r-1)<RT65202eTo6bV0g~, 021400323<2ahL0NP86a698063*56076

"10:L/n/".gen errors: [:value]
#> 1hwIw;v{KQ, mpk*l]!7:!, wocipgZt8@

Validate if a string is following a pattern

If you need to validate if a specific text is fulfilling the pattern you can use the validate method.

If a string pattern supplied and no other parameters supplied the output will be an array with the errors detected.

Possible output values, empty array (validation without errors detected) or one or more of: :min_length, :max_length, :length, :value, :string_set_not_allowed, :required_data, :excluded_data

In case an array of patterns supplied it will return only true or false

Examples:

#StringPattern class
StringPattern.validate((text: "This text will be validated", pattern: :"10-20:Xn")
#> [:max_length, :length, :value, :string_set_not_allowed]

#String class
"10:N".validate "333444"
#> [:min_length, :length]

#Symbol class
:"10:N".validate("333444")
#> [:min_length, :length]

#Array class
["5:L","3:xn","4-10:n"].validate "DjkljFFc343444390"
#> false

If we want to validate a string with a pattern and we are expecting to get specific errors, you can supply the parameter expected_errors (alias: errors) or not_expected_errors (aliases: non_expected_errors, not_errors).

In this case, the validate method will return true or false.

Examples:

"10:N".val "3445", errors: [:min_length]
#> true

"10:N/[09]/".validate "4434039440", errors: [:value]
#> false

"10-12:XN/x/".validate "FDDDDDAA343434", errors: [:max_length, :required_data]
#> true

Configure

SP_ADD_TO_RUBY

This gem adds the methods generate (alias: gen) and validate (alias: val) to the Ruby classes: String, Array, and Symbol.

Also adds the method generate (alias: gen) to Kernel. By default (true) it is always added.

In case you don't want to be added, just before requiring the library set:

SP_ADD_TO_RUBY = false
require 'string_pattern'

In case it is set to true (default) then you will be able to use:

require 'string_pattern'

#String object
"20-30:@".gen 
#>dkj34MljjJD-df@jfdluul.dfu

"10:L/N/[/-./%d%]".validate("12ds6f--.s") 
#>[:value, :string_set_not_allowed]

"20-40:@".validate(my_email)

#Kernel
gen "10:N"
#>3433409877

#Array object
"(,3:N,) ,3:N,-,2:N,-,2:N".split(",").generate 
#>(937) 980-65-05

%w{( 3:N ) 1:_ 3:N - 2:N - 2:N}.gen 
#>(045) 448-63-09

["1:L", "5-10:LN", "-", "3:N"].gen 
#>zqWihV-746

national_chars

To specify which national characters will be used when using the symbol type: T, you use StringPattern.national_chars, by default is the English alphabet

StringPattern.national_chars = (('a'..'z').to_a + ('A'..'Z').to_a).join + "áéíóúÁÉÍÓÚüÜñÑ"
"10-20:Tn".gen #>AAñ34Ef99éNOP

optimistic

If true it will check on the strings of the array positions supplied if they have the pattern format and assume in that case that is a pattern. If not it will assume the patterns on the array will be supplied as symbols. By default is set to true.

StringPattern.optimistic = false
["5:X","fixedtext", "3:N"].generate
#>5:Xfixedtext3:N
[:"5:X","fixedtext", :"3:N"].generate
#>AUJKJfixedtext454

StringPattern.optimistic = true
["5:X","fixedtext", "3:N"].generate
#>KKDMEfixedtext344
[:"5:X","fixedtext", :"3:N"].generate
#>SAAERfixedtext988

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.

License

The gem is available as open source under the terms of the MIT License.


Author: MarioRuiz
Source code: https://github.com/MarioRuiz/string_pattern
License: MIT license

#ruby  #ruby-on-rails 

田辺  亮介

田辺 亮介

1660032308

輕鬆過濾 Pandas 數據框的 14 種方法

每當我們處理任何類型的數據時,我們都需要清楚地了解我們正在處理的數據類型。對於那裡的大多數數據,其中可能包含數千甚至數百萬個包含各種信息的條目,如果沒有任何工具以簡短易讀的格式呈現數據,就真的不可能理解這些數據。

大多數時候,我們需要瀏覽數據、操作數據並將其可視化以獲得洞察力。嗯,有一個很棒的庫,它的名字叫 pandas,它為我們提供了這種能力。最常見的數據操作操作是數據過濾。它與 SQL 中的 WHERE 子句非常相似,或者您必須在 MS Excel 中使用過濾器來根據某些條件選擇特定行。

pandas是一個強大、靈活和開源的數據分析/操作工具,它本質上是一個python 包,提供速度、靈活性和富有表現力的數據結構,以直觀和簡單的方式處理關係”或“標記它是在 Python 中執行實際數據分析的最流行的庫

pandas建立在 NumPy 庫之上,旨在與科學計算環境和眾多其他第三方庫很好地集成。它有兩個主要數據結構,即Series (1D)Dataframes(2D),在大多數實際用例中,這是金融、科學計算、工程和統計等許多領域正在處理的數據類型。

讓我們開始在 Pandas Dataframe 的幫助下過濾數據

安裝熊貓

!pip install pandas

導入 Pandas 庫,讀取我們的示例數據文件並將其分配給“df” DataFrame

import pandas as pd
df = pd.read_csv(r"C:\Users\rajam\Desktop\sample_data.csv")

讓我們看看我們的數據框

print(df.head())

樣本數據

樣本數據

現在我們有了 DataFrame,我們將應用各種方法來過濾它。

方法 - 1:按列值過濾 DataFrame

我們的 DataFrame 中有一個名為“Total_Sales”的列,我們想要過濾掉所有大於 300 的銷售額。

#Filter a DataFrame for a single column value with a given condition
 
greater_than = df[df['Total_Sales'] > 300]
print(greater_than.head())

銷售額 > 300 的 Sample_data

銷售額超過 300

Method – 2 : Filtering DataFrame based on multiple conditions

在這裡,我們過濾“Total_Sales”值大於 300 以及“Units”大於 20 的所有值。我們將不得不使用執行按位與操作的 python 運算符“&”以顯示相應的結果。

#Filter a DataFrame with multiple conditions
 
filter_sales_units = df[(df['Total_Sales'] > 300) & (df["Units"] > 20)]
print(Filter_sales_units.head())

圖 3

篩選銷售額和單位

方法 – 3:根據日期值過濾 DataFrame

如果我們想根據某個日期值過濾我們的數據框,例如這裡我們試圖獲取基於特定日期的所有結果,在我們的例子中是日期 '03/10/21' 之後的結果。

#Filter a DataFrame based on specific date
 
date_filter = df[df['Date'] > '03/10/21']
print(date_filter.head())

圖 1

按日期過濾

方法四:基於Date值多條件過濾DataFrame

在這裡,我們得到了評估多個日期的 Date 操作的所有結果。

#Filter a DataFrame with multiple conditions our Date value
 
date_filter2 = df[(df['Date'] >= '3/25/2021') & (df['Date'] <'8/17/2021')]
print(date_filter2.head())

圖 2

篩選具有多個條件的日期

方法五:根據特定字符串過濾DataFrame

在這裡,我們選擇一個名為“Region”的列並獲取來自“East”區域的所有行,從而根據特定的字符串值進行過濾。

#Filter a DataFrame to a specific string
 
east = df[df['Region'] == 'East']
print(east.head())

圖 6

根據特定字符串過濾

方法6:根據字符串中的特定索引值過濾 DataFrame

在這裡,我們選擇一個名為“Region”的列,並獲取所有以字母“E”作為第一個字符的行,即指定列結果中索引 0 處的所有行。

#Filter a DataFrame to show rows starting with a specfic letter
 
starting_with_e = df[df['Region'].str[0]== 'E']
print(starting_with_e.head())

圖 7

根據特定字母過濾

方法7:根據值列表過濾 DataFrame

在這裡,我們過濾包含值“West”和“East”的“Region”列中的行,並顯示組合結果。可以使用兩種方法來執行此過濾,即使用管道 | 具有相應所需值集的運算符具有以下語法,或者我們可以使用.isin()函數過濾給定列中的值,在我們的例子中是“區域”,並提供所需集的列表它裡面的值作為一個列表。

#Filter a DataFrame rows based on list of values
 
#Method 1:
east_west = df[(df['Region'] == 'West') | (df['Region'] == 'East')]
print(east_west)
 
#Method 2:
east_west_1 = df[df['Region'].isin(['West', 'East'])]
print(east_west_1.head())

圖 9

方法-2的輸出

方法 – 8:使用 RegEx 根據特定值過濾 DataFrame 行

在這裡,我們想要列 'Region' 中的所有值,並在其字符串值中以 'th'結尾並顯示它們。換句話說,我們希望我們的結果顯示 'Nor th ' 和 'Sout th ' 的值並忽略 'East' 和 'West'。具有指定值的方法.str.contains()以及$ RegEx 模式可用於獲得所需的結果。

有關更多信息,請查看正則表達式文檔

#Filtering the DataFrame rows using regular expressions(REGEX)
 
regex_df = df[df['Region'].str.contains('th$')]
print(regex_df.head())

圖 10

基於 REGEX 的過濾器

方法9:過濾 DataFrame 以檢查null

在這裡,我們將在isnull() 函數的幫助下檢查所有列中的空值和非空值。

#Filtering to check for null and not null values in all columns
 
df_null = df[df.isnull().any(axis=1)]
print(df_null.head())

圖 12

基於 NULL 或 NOT 空值過濾

方法 - 10:過濾 DataFrame 以檢查特定列中的空值。

#Filtering to check for null values if any in the 'Units' column
 
units_df = df[df['Units'].isnull()]
print(units_df.head())

圖 13

在特定列上查找空值

方法 – 11:過濾 DataFrame 以檢查特定列中的非 空值

#Filtering to check for not null values in the 'Units' column
 
df_not_null = df[df['Units'].notnull()]
print(df_not_null.head())

圖 14

在特定列上查找非空值

Method – 12: Filtering DataFrame using query()with a condition

#Using query function in pandas
 
df_query = df.query('Total_Sales > 300')
print(df_query.head())

圖 17

Query使用函數過濾值

Method – 13: Filtering DataFrame using query()with multiple conditions

#Using query function with multiple conditions in pandas
 
df_query_1 = df.query('Total_Sales > 300 and Units <18')
print(df_query_1.head())

圖 18

Query使用函數過濾多列

方法 –loc 14:使用和iloc函數過濾我們的 DataFrame 。

#Creating a sample DataFrame for illustrations
 
import numpy as np
data = pd.DataFrame({"col1" : np.arange(1, 20 ,2)}, index=[19, 18 ,8, 6, 0, 1, 2, 3, 4, 5])
print(data)

圖 19

樣本數據

解釋iloc 根據給定索引的位置考慮行,因此它僅將整數作為值。

有關更多信息,請查看Pandas 文檔

#Filter with iloc
 
data.iloc[0 : 5]

圖 20

過濾使用iloc

說明loc 考慮基於索引標籤的行

#Filter with loc
 
data.loc[0 : 5]

圖 21

過濾使用loc

您可能正在思考為什麼loc函數返回 6 行而不是 5 行。這是因為不會根據索引位置產生輸出。它只考慮索引標籤,它也可以是字母表,包括起點和終點。 loc 

結論

因此,這些是 pandas 中最常用的一些過濾方法。還有許多其他過濾方法可以使用,但這些是最常見的一些。

鏈接:https ://www.askpython.com/python-modules/pandas/filter-pandas-dataframe

#pandas #python #datafame

August  Larson

August Larson

1660147320

Top 14 Ways to Filter Pandas Dataframes Easily

Whenever we work with data of any sort, we need a clear picture of the kind of data that we are dealing with. For most of the data out there, which may contain thousands or even millions of entries with a wide variety of information, it’s really impossible to make sense of that data without any tool to present the data in a short and readable format.

Most of the time we need to go through the data, manipulate it, and visualize it for getting insights. Well, there is a great library which goes by the name pandas which provides us with that capability. The most frequent Data manipulation operation is Data Filtering. It is very similar to the WHERE clause in SQL or you must have used a filter in MS Excel for selecting specific rows based on some conditions.

pandas is a powerful, flexible and open source data analysis/manipulation tool which is essentially a python package that provides speed, flexibility and expressive data structures crafted to work with “relational” or “labelled” data in an intuitive and easy manner. It is one of the most popular libraries to perform real-world data analysis in Python.

pandas is built on top of the NumPy library which aims to integrate well with the scientific computing environment and numerous other 3rd party libraries. It has two primary data structures namely Series (1D) and Dataframes(2D), which in most real-world use cases is the type of data that is being dealt with in many sectors of finance, scientific computing, engineering and statistics.

Let’s Start Filtering Data With the Help of Pandas Dataframe

Installing pandas

!pip install pandas

Importing the Pandas library, reading our sample data file and assigning it to “df” DataFrame

import pandas as pd
df = pd.read_csv(r"C:\Users\rajam\Desktop\sample_data.csv")

Let’s check out our dataframe:

print(df.head())

Sample_data

Sample_data

Now that we have our DataFrame, we will be applying various methods to filter it.

Method – 1: Filtering DataFrame by column value

We have a column named “Total_Sales” in our DataFrame and we want to filter out all the sales value which is greater than 300.

#Filter a DataFrame for a single column value with a given condition
 
greater_than = df[df['Total_Sales'] > 300]
print(greater_than.head())

Sample_data with sales > 300

Sales with Greater than 300

Method – 2: Filtering DataFrame based on multiple conditions

Here we are filtering all the values whose “Total_Sales” value is greater than 300 and also where the “Units” is greater than 20. We will have to use the python operator “&” which performs a bitwise AND operation in order to display the corresponding result.

#Filter a DataFrame with multiple conditions
 
filter_sales_units = df[(df['Total_Sales'] > 300) & (df["Units"] > 20)]
print(Filter_sales_units.head())

Image 3

Filter on Sales and Units

Method – 3: Filtering DataFrame based on Date value

If we want to filter our data frame based on a certain date value, for example here we are trying to get all the results based on a particular date, in our case the results after the date ’03/10/21′.

#Filter a DataFrame based on specific date
 
date_filter = df[df['Date'] > '03/10/21']
print(date_filter.head())

Image 1

Filter on Date

Method – 4: Filtering DataFrame based on Date value with multiple conditions

Here we are getting all the results for our Date operation evaluating multiple dates.

#Filter a DataFrame with multiple conditions our Date value
 
date_filter2 = df[(df['Date'] >= '3/25/2021') & (df['Date'] <'8/17/2021')]
print(date_filter2.head())

Image 2

Filter on a date with multiple conditions

Method – 5: Filtering DataFrame based on a specific string

Here we are selecting a column called ‘Region’ and getting all the rows that are from the region ‘East’, thus filtering based on a specific string value.

#Filter a DataFrame to a specific string
 
east = df[df['Region'] == 'East']
print(east.head())

Image 6

Filter based on a specific string

Method – 6: Filtering DataFrame based on a specific index value in a string

Here we are selecting a column called ‘Region’ and getting all the rows which has the letter ‘E’ as the first character i.e at index 0 in the specified column results.

#Filter a DataFrame to show rows starting with a specfic letter
 
starting_with_e = df[df['Region'].str[0]== 'E']
print(starting_with_e.head())

Image 7

Filter based on a specific letter

Method – 7: Filtering DataFrame based on a list of values

Here we are filtering rows in the column ‘Region’ which contains the values ‘West’ as well as ‘East’ and display the combined result. Two methods can be used to perform this filtering namely using a pipe | operator with the corresponding desired set of values with the below syntax OR we can use the .isin() function to filter for the values in a given column, which in our case is the ‘Region’, and provide the list of the desired set of values inside it as a list.

#Filter a DataFrame rows based on list of values
 
#Method 1:
east_west = df[(df['Region'] == 'West') | (df['Region'] == 'East')]
print(east_west)
 
#Method 2:
east_west_1 = df[df['Region'].isin(['West', 'East'])]
print(east_west_1.head())

Image 9

Output of Method -2

Method – 8: Filtering DataFrame rows based on specific values using RegEx

Here we want all the values in the column ‘Region’, which ends with ‘th’ in their string value and display them. In other words, we want our results to show the values of ‘North‘ and ‘South‘ and ignore ‘East’ and ‘West’. The method .str.contains() with the specified values along with the $ RegEx pattern can be used to get the desired results.

For more information please check the Regex Documentation

#Filtering the DataFrame rows using regular expressions(REGEX)
 
regex_df = df[df['Region'].str.contains('th$')]
print(regex_df.head())

Image 10

Filter based on REGEX

Method – 9: Filtering DataFrame to check for null

Here, we’ll check for null and not null values in all the columns with the help of isnull() function.

#Filtering to check for null and not null values in all columns
 
df_null = df[df.isnull().any(axis=1)]
print(df_null.head())

Image 12

Filter based on NULL or NOT null values

Method – 10: Filtering DataFrame to check for null values in a specific column.

#Filtering to check for null values if any in the 'Units' column
 
units_df = df[df['Units'].isnull()]
print(units_df.head())

Image 13

Finding null values on specific columns

Method – 11: Filtering DataFrame to check for not null values in specific columns

#Filtering to check for not null values in the 'Units' column
 
df_not_null = df[df['Units'].notnull()]
print(df_not_null.head())

Image 14

Finding not-null values on specific columns

Method – 12: Filtering DataFrame using query() with a condition

#Using query function in pandas
 
df_query = df.query('Total_Sales > 300')
print(df_query.head())

Image 17

Filtering values with Query Function

Method – 13: Filtering DataFrame using query() with multiple conditions

#Using query function with multiple conditions in pandas
 
df_query_1 = df.query('Total_Sales > 300 and Units <18')
print(df_query_1.head())

Image 18

Filtering multiple columns with Query Function

Method – 14: Filtering our DataFrame using the loc and iloc functions.

#Creating a sample DataFrame for illustrations
 
import numpy as np
data = pd.DataFrame({"col1" : np.arange(1, 20 ,2)}, index=[19, 18 ,8, 6, 0, 1, 2, 3, 4, 5])
print(data)

Image 19

sample_data

Explanation: iloc considers rows based on the position of the given index, so that it takes only integers as values.

For more information please check out Pandas Documentation

#Filter with iloc
 
data.iloc[0 : 5]

Image 20

Filter using iloc

Explanation: loc considers rows based on index labels

#Filter with loc
 
data.loc[0 : 5]

Image 21

Filter using loc

You might be thinking about why the loc function returns 6 rows instead of 5 rows. This is because loc does not produce output based on index position. It considers labels of index only which can be an alphabet as well and includes both starting and endpoint.

Conclusion

So, these were some of the most common filtering methods used in pandas. There are many other filtering methods that could be used, but these are some of the most common.

Link: https://www.askpython.com/python-modules/pandas/filter-pandas-dataframe

#pandas #python #datafame

14 лучших способов легко фильтровать кадры данных Pandas

Всякий раз, когда мы работаем с данными любого рода, нам нужна четкая картина того, с какими данными мы имеем дело. Для большинства имеющихся данных, которые могут содержать тысячи или даже миллионы записей с разнообразной информацией, действительно невозможно разобраться в этих данных без какого-либо инструмента для представления данных в кратком и удобочитаемом формате.

Большую часть времени нам нужно просматривать данные, манипулировать ими и визуализировать их для получения информации. Что ж, есть отличная библиотека под названием pandas, которая предоставляет нам эту возможность. Наиболее частой операцией манипулирования данными является фильтрация данных. Это очень похоже на предложение WHERE в SQL, или вы должны были использовать фильтр в MS Excel для выбора определенных строк на основе некоторых условий.

pandas — это мощный, гибкий инструмент с открытым исходным кодом для анализа/манипулирования данными, который, по сути, представляет собойпакет Python, обеспечивающий скорость, гибкость и выразительные структуры данных, созданные для интуитивно понятной и простой работы с «реляционными» или «помеченными» данными. Это одна из самых популярных библиотекдля реального анализа данных в Python.

pandas построен на основе библиотеки NumPy, которая нацелена на хорошую интеграцию с научной вычислительной средой и множеством других сторонних библиотек. Он имеет две основные структуры данных, а именно Series (1D) и Dataframes(2D) , которые в большинстве реальных случаев использования представляют собой тип данных, с которыми имеют дело во многих секторах финансов, научных вычислений, инженерии и статистики.

Давайте начнем фильтровать данные с помощью Pandas Dataframe

Установка панд

!pip install pandas

Импорт библиотеки Pandas, чтение нашего примера файла данных и назначение его в «df» DataFrame

import pandas as pd
df = pd.read_csv(r"C:\Users\rajam\Desktop\sample_data.csv")

Давайте проверим наш фрейм данных :

print(df.head())

Образец данных

Образец данных

Теперь, когда у нас есть DataFrame, мы будем применять различные методы для его фильтрации.

Метод — 1 : фильтрация DataFrame по значению столбца

У нас есть столбец с именем «Total_Sales» в нашем DataFrame, и мы хотим отфильтровать все значения продаж, превышающие 300.

#Filter a DataFrame for a single column value with a given condition
 
greater_than = df[df['Total_Sales'] > 300]
print(greater_than.head())

Sample_data с продажами > 300

Продажи с более чем 300

Метод — 2 : фильтрация DataFrame на основе нескольких условий

Здесь мы фильтруем все значения, у которых значение «Total_Sales» больше 300, а также где «Единицы» больше 20. Нам нужно будет использовать оператор Python «&», который выполняет побитовую операцию И, чтобы отобразить соответствующий результат.

#Filter a DataFrame with multiple conditions
 
filter_sales_units = df[(df['Total_Sales'] > 300) & (df["Units"] > 20)]
print(Filter_sales_units.head())

Изображение 3

Фильтр по продажам и единицам

Метод — 3 : фильтрация DataFrame на основе значения даты

Если мы хотим отфильтровать наш фрейм данных на основе определенного значения даты, например, здесь мы пытаемся получить все результаты на основе определенной даты, в нашем случае результаты после даты «10.03.21».

#Filter a DataFrame based on specific date
 
date_filter = df[df['Date'] > '03/10/21']
print(date_filter.head())

Изображение 1

Фильтр по дате

Метод — 4: фильтрация DataFrame на основе значения даты с несколькими условиями

Здесь мы получаем все результаты нашей операции Date, оценивающей несколько дат .

#Filter a DataFrame with multiple conditions our Date value
 
date_filter2 = df[(df['Date'] >= '3/25/2021') & (df['Date'] <'8/17/2021')]
print(date_filter2.head())

Изображение 2

Фильтр по дате с несколькими условиями

Метод — 5: фильтрация DataFrame на основе определенной строки

Здесь мы выбираем столбец под названием «Регион» и получаем все строки из региона «Восток», таким образом фильтруя на основе определенного строкового значения .

#Filter a DataFrame to a specific string
 
east = df[df['Region'] == 'East']
print(east.head())

Изображение 6

Фильтровать по определенной строке

Метод — 6: фильтрация DataFrame на основе определенного значения индекса в строке

Здесь мы выбираем столбец под названием «Регион» и получаем все строки, в которых буква «Е» является первым символом, т.е. индексом 0 в результатах указанного столбца.

#Filter a DataFrame to show rows starting with a specfic letter
 
starting_with_e = df[df['Region'].str[0]== 'E']
print(starting_with_e.head())

Изображение 7

Фильтр по определенной букве

Метод — 7: Фильтрация DataFrame на основе списка значений

Здесь мы фильтруем строки в столбце «Регион», который содержит значения «Запад», а также «Восток», и отображаем объединенный результат. Для выполнения этой фильтрации можно использовать два метода, а именно использование канала | оператор с соответствующим желаемым набором значений с приведенным ниже синтаксисом ИЛИ мы можем использовать функцию .isin() для фильтрации значений в данном столбце, которым в нашем случае является «Регион», и предоставить список желаемого набора значений внутри него в виде списка.

#Filter a DataFrame rows based on list of values
 
#Method 1:
east_west = df[(df['Region'] == 'West') | (df['Region'] == 'East')]
print(east_west)
 
#Method 2:
east_west_1 = df[df['Region'].isin(['West', 'East'])]
print(east_west_1.head())

Изображение 9

Выход метода -2

Метод — 8: фильтрация строк DataFrame на основе определенных значений с использованием RegEx

Здесь нам нужны все значения в столбце «Регион» , которые заканчиваются на «th» в их строковом значении, и отобразить их. Другими словами, мы хотим, чтобы наши результаты показывали значения «Север » и «Юг » и игнорировали «Восток» и «Запад» . Метод .str.contains() с указанными значениями вместе с шаблоном $ RegEx можно использовать для получения желаемых результатов.

Для получения дополнительной информации ознакомьтесь с документацией по регулярным выражениям.

#Filtering the DataFrame rows using regular expressions(REGEX)
 
regex_df = df[df['Region'].str.contains('th$')]
print(regex_df.head())

Изображение 10

Фильтр на основе REGEX

Метод — 9: фильтрация DataFrame для проверки на нуль

Здесь мы проверим нулевые и не нулевые значения во всех столбцах с помощью функции isnull() .

#Filtering to check for null and not null values in all columns
 
df_null = df[df.isnull().any(axis=1)]
print(df_null.head())

Изображение 12

Фильтр на основе значений NULL или NOT null

Метод — 10: фильтрация DataFrame для проверки нулевых значений в определенном столбце.

#Filtering to check for null values if any in the 'Units' column
 
units_df = df[df['Units'].isnull()]
print(units_df.head())

Изображение 13

Поиск нулевых значений в определенных столбцах

Метод — 11: фильтрация DataFrame для проверки ненулевых значений в определенных столбцах

#Filtering to check for not null values in the 'Units' column
 
df_not_null = df[df['Units'].notnull()]
print(df_not_null.head())

Изображение 14

Поиск ненулевых значений в определенных столбцах

Метод — 12: Фильтрация DataFrame query()с использованием условия

#Using query function in pandas
 
df_query = df.query('Total_Sales > 300')
print(df_query.head())

Изображение 17

Фильтрация значений с Queryфункцией

Метод — 13: фильтрация DataFrame с использованием query()нескольких условий

#Using query function with multiple conditions in pandas
 
df_query_1 = df.query('Total_Sales > 300 and Units <18')
print(df_query_1.head())

Изображение 18

Фильтрация нескольких столбцов с Queryфункцией

Метод — 14: фильтрация нашего DataFrame с использованием функций locи .iloc

#Creating a sample DataFrame for illustrations
 
import numpy as np
data = pd.DataFrame({"col1" : np.arange(1, 20 ,2)}, index=[19, 18 ,8, 6, 0, 1, 2, 3, 4, 5])
print(data)

Изображение 19

образец данных

Объяснение : iloc считает строки на основе позиции заданного индекса, поэтому в качестве значений принимает только целые числа.

Для получения дополнительной информации ознакомьтесь с документацией Pandas.

#Filter with iloc
 
data.iloc[0 : 5]

Изображение 20

Фильтровать с помощьюiloc

Объяснение : loc считает строки на основе меток индекса .

#Filter with loc
 
data.loc[0 : 5]

Изображение 21

Фильтровать с помощьюloc

Вы можете подумать, почему locфункция возвращает 6 строк вместо 5 строк. Это связано с тем , что вывод не производится на основе позиции индекса. Он рассматривает только метки индекса, которые также могут быть алфавитом, и включает как начальную, так и конечную точку. loc 

Вывод

Итак, это были одни из наиболее распространенных методов фильтрации, используемых в пандах. Существует множество других методов фильтрации, которые можно использовать, но эти являются одними из наиболее распространенных.

Ссылка: https://www.askpython.com/python-modules/pandas/filter-pandas-dataframe

#pandas #python #datafame