What are Sets in Python and How to use them? NareshIT
Sets in Python

What are Sets in Python and How to use them? NareshIT

Introduction:

The Python is more powerful language which offer great tools for data crunching and preparation, as well as for complex scientific data analysis and modelling. In Python the round() is a built-in?function which when get used then it will return a float number. In this case If the?decimal?places to be rounded are not specified by the user then it is considered as 0, and it will?round?to the nearest integer.

Python round():

As I have already discussed above that round() function in python is the inbuilt function which is used to return the rounded equivalent of decimal point values.It should be get noted that in the round() if we don’t provide the respective value (i.e., the number of digits after decimal) then it will round off the number to the nearest integer.

Syntax:

round(x, n)

Let us consider the following example which will let you know about the use of round() function in Python .

print(round(7.6 + 8.7,1))

Here the output will be 16.3.

Similarly, if we are going to have the case as

print(round(6.543231, 2))then the output will be 6.54.

Practical Applications:

As we have discussed few examples above and we saw that how the round() is being used. It should be get noted that the applications of round functions are rounding digits to limited numbers.

In above we have consider an example, where we are usually taking 2 or 3 numbers after decimal.

Let us consider another example as below as

b=2/6

print(b)

print(round(b, 2))

When this program will run then it will produce the output as 0.33

Rounding NumPy Arrays:

When we are going to use the numpy then if we are using the Python 3 then no need to install but if you are using the Python2 then first you need to install NumPy. To install the numpy you can use the pip:

pip install numpy

Once the numpy is get installed into your environment then to round all the values of NumPy array we will pass data as an argument to np.around() function.

Let us consider the following example where we will create a NumPy array of 3×4 size containing floating-point numbers. The program is as discussed below:

Program:

import numpy as np

np.random.seed(444)

data = np.random.randn(3, 4)

print(data)

When the above program will run then it will produce the output as

[[ 0.35743992 0.3775384 1.38233789 1.17554883]

[-0.9392757 -1.14315015 -0.54243951 -0.54870808]

[ 0.20851975 0.21268956 1.26802054 -0.80730293]]

Like the above the numpy also have the other functions which are most frequently get used. Such as numpy.ceil(), numpy.floor(), numpy.trunc(), and numpy.rint().They are used in the following manner as discussed below.

1.?????print(np.ceil(data)) # This function rounds every value in the array to the nearest integer greater than or equal to the original value.

2.?????print(np.floor(data)) # it is basically used to round every value down to the nearest integer.

3.?????print(np.trunc(data))# This function is basically get used to truncate each value to its integer component.

4.?????print(np.rint(data))# it is basically used to round to the nearest integer using the “rounding half to even” strategy.

Rounding Pandas Series and DataFrame:

Pandas is the most important and popular library in Python which is basically get used when we are going to analyse the data and interpret the data.?Here to analyse the data more properly we can also take the help of numpy. But it should be get noted that the Numpy and pandas are to be get installed in your system. If you need to install the Pandas then it can be get done using following manner.

pip install pandas

Let us consider the following example which will let you know about the detailed semantics of pandas series data frame.

import pandas as pd

import numpy as np

np.random.seed(444)

series = pd.Series(np.random.randn(4))

print(series)

Here when the program will run then the output obtained will be as follows,

0 0.357440

1 0.377538

2 1.382338

3 1.175549

dtype: float64

print(series.round(2))

0 0.36

1 0.38

2 1.38

3 1.18

dtype: float64

It should also be get noted that the DataFrame is basically like a table in database and Series is a column. We can round objects using Series.round() andDataFrame.round() method.

DataFrame:

As we have already discussed about the Pandas series above but if we are using the DataFrame then here we can specify different precision for each column if we need for the individual.

It should be get noted that after doing the above we can apply the round() function, which is allowed to accept a dictionary or Series. Here by using this we can be able to provide different precision for different columns specifically using the following syntax.

round({“Column 1”: 1, “Column 2”: 2, “Column 3”: 3})

Let us consider the following program which will let you understand in detail about the use and implementation of DataFrame.

Program:

import pandas as pd

import numpy as np

np.random.seed(444)

df = pd.DataFrame(np.random.randn(3, 3), columns=["Column 1", "Column 2", "Column 3"])

print(df)

print(df.round(3))

When this program will run then the output gets observed will be as discussed below.

Column 1 Column 2 Column 3

0 ???????0.4 ???????0.38 ???1.382

1 ???????1.2 ??????-0.94??? -1.143

2????????-0.5 ?????-0.55 ??0.209

?Scope @ NareshIT:

1.?????At NareshIT’s Python application Development program you will be able to get the extensive hands-on training in front-end, middleware, and back-end technology.

2.?????It skilled you along with phase-end and capstone projects based on real business scenarios.

3.?????Here you learn the concepts from leading industry experts with content structured to ensure industrial relevance.

4.?????An end-to-end application with exciting features

You can contact us anytime for your?Python training , and from any part of the world.?Naresh I Technologies ?caters to one of the best Python training in India.

Follow us on Linkedin:?https://bit.ly/NITLinkedIN

要查看或添加评论,请登录

社区洞察

其他会员也浏览了