Listado de la etiqueta: Multiple


We will be discussing Pandas in Python, an open-source library that delivers high-performance data structures and data analysis tools that are ready to use. We will also learn about the DataFrame, the advantages of Pandas, and how you can use Pandas to select multiple columns of a DataFrame . Let’s get started!

What is Pandas in Python?

Pandas is a Python open-source library. It delivers efficient structures and tools for data analysis that are ready to use. Pandas is a Python module that operates on top of NumPy and is widely used for data science and analytics. NumPy is another set of low-level data structures that can handle multi-dimensional arrays and a variety of mathematical array operations. Pandas have a more advanced user interface. It also has robust time-series capability and efficient tabular data alignment. Pandas’ primary data structure is the DataFrame. A 2-D data structure allows us to store and modify tabular data. Pandas provide any functionality to the DataFrame like data manipulation, concatenation, merging, grouping, etc.

What is a DataFrame?

The most essential and extensively used data structure is the DataFrame. It is a common method of data storage. DataFrame stores data in rows and columns, just like an SQL table or a spreadsheet database.

Advantages of Pandas

Many users wish that the SQL have included capabilities like the Gaussian random number generation or quantiles because they struggle to incorporate a procedural notion into an SQL query. Users may say, “If only I could write this in Python and switch back to SQL quickly,” and Pandas provides a tabular data type with well-designed interfaces that allow them to do exactly that. There are more verbose options, such as utilizing a specific procedural language like the Oracle’s PLSQL or Postgres’ PLPGSQL or a low-level database interface. Pandas have a one-liner SQL read interface (pd.read sql) and a one-liner SQL write interface (pd.to sql), comparable to R data frames.

Another significant advantage is that the charting libraries such as Seaborn may treat the data frame columns as high-level graph attributes. So, Pandas provide a reasonable way of managing the tabular data in Python and some very wonderful storage and charting APIs.

Option 1: Using the Basic Key Index

1
2
3
4
5
6
7
8
9
10

import pandas as pd

 

data = {‘Name’:[‘A’, ‘B’, ‘C’, ‘D’],
        ‘Age’:[27, 24, 22, 32]}
 
df = pd.DataFrame(data)
 
df[[‘Name’, ‘Age’]]

 

Output:

1
2
3
4
5
6
7
8
9

    Name     Age

0    A             27

1    B             24

2    C             22

3    D             32

Option 2: Using .loc[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14

import pandas as pd

 

data = {‘Fruit’:[‘Apple’, ‘Banana’, ‘Grapes’, ‘Orange’],
        ‘Price’:[160, 100, 60, 80]}

 

df = pd.DataFrame(data)

 

df.loc[0:2, [‘Fruit’, ‘Price’]]

 

Output:

1
2
3
4
5
6
7
8
9

    Fruit    Price

0  Apple     160

1  Banano    100

2  Grapes    60

3  Orange    80

Option 3: Using .iloc[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

import pandas as pd

 

 

data = {‘Dog’:[‘A’, ‘B’, ‘C’, ‘D’],
        ‘Age’:[2, 4, 3, 1]}

 

 

df = pd.DataFrame(data)

 

df.iloc[:, 0:2]

 

 

Output:

1
2
3
4
5
6
7
8
9

    Dog   Age

0    A     2

1    B     4

2    C     3

3    D     1

Options 4: Using .ix[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

import pandas as pd

 

 

data = {‘Name’:[‘A’, ‘B’, ‘C’, ‘D’],
        ‘Roll number’:[21, 25, 19, 49]}

 

 

df = pd.DataFrame(data)

 

print(df.ix[:, 0:2])

Output:

1
2
3
4
5
6
7
8
9

    Name   Roll number

0   A       21

1   B       25

2   C       19

3   D       49

Conclusion

We discussed about Pandas in Python, the DataFrame, the advantages of Pandas, and how to use Pandas to select multiple columns of a DataFrame. There are four options that we discussed in selecting multiple columns: using the basic key indexing, “.ix”, “.loc”, and “.iloc”, respectively.



Source link


MongoDB database plays an important role in data storing and manipulating. To organize data, we create groups to gather the same sort of data in one place. Grouping can be on different attributes, whether from the count variable or any other feature. This tutorial will explain the group creation according to different fields of documents.

For the implementation of the phenomenon of groups according to multiple fields, we need to have some data in the database. We will create a database first. This is done by declaring the name of the database with the keyword “use.” For this implementation, we are using a database “demo.”

Merienda you are done with the database creation, data will be inserted into the database. And for the data entry we used to create “collections,” these are the containers that play an important role in storing limitless data in them. At a time, we can create many collections in a single database. Here we will create a database with the name “info.”

>> Db.createCollection(‘info’)

The response of MongoDB will be, “ok”; it is the confirmation of the creation of the collection. The data in the collection is entered row by row. So we will insert data into the collection. As this data will be used further in examples to create groups according to different fields, so we have entered many rows. Each time a different id is assigned to each row.

>>  db.info.insertOne ({«Name» : «Savid»,
«Age» : 28,
«Gender» : «Male»,
«Country»: «United States of America»})

Similarly, all the data will be inserted. You can see all inserted data by using the find() command

>> db.info.find().pretty()

Example 1: Group by Multiple Fields/Attributes

When we have a large set of data in the database, but we want to take a view of a few of them, then for this purpose, $groups are found. In this example, we will create a group to see some particular attributes from the collection. The group creador relies on the aggregate operation. An aggregate operation is used, to sum up the data according to the common fields. The Dollar “$” sign denotes the variable. Now apply a query on the above info collection.

A group depending on id, will be created. And then, only the age and gender documents are selected to be displayed. Whereas the entire data, including the name and country, are removed. This is somehow a filter that is used to limit the display of data.

>> db.info.aggregate([ {$group: {_id: {age:«$Age»,  gender:«$Gender»} } } ])

You can see that we have grouped each row according to id by limiting the data to two attributes.

Example 2: Group Through Multiple Fields by Applying a Condition

This refers to the grouping of the documents according to a specific condition. A group will be created on two attributes, and after the group creation, we will add a count variable to count the occurrence of the value of a specific document. And also, we have added a sorting order.

First, let us display the documents in our collection “new.” We have created a collection and added data to it earlier by following the same steps described above. We will only display all the items in the collection through the find() function.

The query will contain the group part first. The group is created on id; university and level are the two basic attributes that we want to get displayed. The variable we use gets the value from the collection and then assigns it to the query variable. All the values and conditions are not written directly in the command.

After the group creation, the condition is applied; it is to count and calculate the sum according to the levels of each document. After that, this answer will be arranged in descending order. This is done through the sort() functions. This function contains only two parameters; for the ascending value, it is 1, and for descending, it is -1.

>> db.new.aggregate([  {$group:{ _id:{ «university»:«$university», «level»:«$level»  },         «levelCount»:{«$sum»:1} }},  {«$sort»:{«levelCount»:1}} ])

The descending order will show that the greater amount of the level will be displayed first, and then the smaller one is displayed following the level document.

Example 3: MongoDb BUCKET Group by Multiple Fields

As the name indicates that the groups are found according to the bucket. This is done by creating the bucket aggregation. Bucket aggregation is the process of categorizing the documents into groups. This group acts like buckets. Each document is divided depending on the specific expression.

To elaborate on this concept, we will take a look at a collection we have created, and now we will apply the commands to that. A “draw” collection is created that stores the basic information regarding a person. We have displayed all the 4 rows entered into the collection earlier.

On the above data, we will apply a command to create a bucket (group) having the year as an attribute to group the data. We have also created boundaries in which the year of born and death are mentioned. The conditions applied on this command include the count variable to count the number of occurrences. We have also used a concatenation method here to combine both the first and the second names as strings. And also, the year of birth will be displayed. The id depends on the year.

When we compute this query, the resultant value will show that two rows are grouped depending on the age boundaries we have created.

Conclusion

The MongoDB feature of grouping depending on more than a single field is elaborated in this article by demonstrating the working of the aggregate operation in group creation. Any group function is incomplete without the aggregate feature. The group feature is applied directly through the different fields to limit the exposure of entire data. The grouping via multiple fields is also accomplished by applying a particular condition. In the end, we have described the creation of a bucket group that contains more items like a bucket.



Source link


“While working in the MongoDB database, we tend to utilize the “find” function more often to display the data from the collections as a document. The find() function can be utilized in different ways possible. You can use it to display or restrict the display of the specific number of columns at your output by specifying the column names to 1 or 0, respectively. Along with that, we can also define the multiple conditions within the find() function of MongoDB to restrict the number of records for the collection. Within the SQL database, we try to specify the conditions within the WHERE clause. But, within MongoDB, we need to use other ways. Thus, we have decided to cover those methods in this guide. Let’s start with our new article now. Before diving into the implementation deep down, we have to log in from Ubuntu 20.04 system and open its terminal by the utilization of Ctrl+Alt+T. After opening the shell, it is high time to update our system before going further. These updates are necessary for the smooth implementation of our article. Thus, try the shown below instructions followed by the password of a current user to continue.”

Affirm this action by pressing “y” upon asked. Press Enter to continue. The processing will be displayed on your terminal screen. Within a few seconds, your system will get up to date with the latest versions.

After the update has been completed, we have to launch the MongoDB shell at our Ubuntu 20.04 shell. For this, use the “mongo” keyword command as we did below.

The terminal of MongoDB has been launched and is ready to use. Let’s display the list of available databases of MongoDB in which we want to work via the “show dbs” instruction at its shell area. It will show the total databases available. First, three of them are built-in and used to store configuration data. We will be using the user-defined “test” database in this tutorial. To use the “test” database, try out the “use” instruction with the database name “test.” Press the “Enter” key to execute this instruction.

To try out the multiple conditions within the find() function of MongoDB, we must have some collection in the “test” database and sufficient records within the collection. Right now, our database is empty. Thus, we need to create a new collection from scratch. We need to try out the “createCollection” function within the “db” instruction, followed by the name of a new collection to be created in the parenthesis. We have named the collection “Data.” The query was successful, and the collection was generated successfully as per the “ok: 1” status.

Now, we have a new and empty collection of “Data” within our database. We need to put some values as a MongoDB document in it. To insert the data within the MongoDB collection, we need to try out the db insertion with the insert() function preceded by the collection name. Thus, we have been using the same “db” instruction with our newly created collection name, i.e., Data and the insert() function taking values within it. We have been adding a different number of columns for each document record. The column names are: “_id,” “Name,” “City,” “Age,” “salary,” and “job.” Not every record contains all the columns, as we have mentioned. But, each record must contain the “_id,” “Name,” City,” and “Age” columns within it. A total of 15 records have been added with this insert() function command, as shown.

Before trying the conditions on the Data collection, we will be simply using the “find” function to fetch all its records at merienda on our screen. So, we have tried the find() function within the “db” command of our MongoDB. This command has been displaying all 15 records.

As we have mentioned before, we can restrict the number of columns to be displayed in our MongoDB shell by the use of options 1 and 0 with the column name. So, we will try that as well. We have been restricting the display of column “_id” at the MongoDB shell by setting the column value of “_id” to 0 within the find() function. It displayed all the columns except “_id.”

Let’s use the conditions in the find() function now. Let’s suppose you want to display the only records from the Data collection where the City is “Paris.” For this, you need to specify the “$or” variable, and within its [] brackets, specify the column name with the value “Paris” as we did in the command displayed below. A total of 2 document records have been found so far.

We can also specify the names of the columns to be displayed within the find() instruction as we have done it within the shown instruction so far. A total of 3 records have been found.

Let’s use more than 1 condition for the same column using the “$or” variable in the find() function. So, we have been searching for the records that contain the “job” column value as “Doctor,” “Engineer,” and “ShopKeeper.” We have also specified the columns to be shown. It displayed a total of 3 records.

Apart from column values, you can use the comparison operators as well. We have been using the less than “lt” operator in the find() function to display the records only where the ID is less than 6. It shows a total of 5 records.

Just like that, we have tried the greater than comparison operator for the “salary” column within the find function and got the 3 records in return.

Conclusion

This article is the best help to show you the use of the find() function with multiple conditions to display records of collection within MongoDB. We have tried to cover the most in our illustrations to make you understand how easy it is to do with the find() instruction. We have tried the column values and the comparison operators to limit the number of records or display the specific document records on the shell, i.e., less than, greater than operators.



Source link