Found 10476 Articles for Python

Explain the different ways in which data from a series data structure can be accessed in Python?

AmitDiwan
Updated on 10-Dec-2020 13:39:25

128 Views

The ability to index elements and access them using their positional index values serves a great purpose when we need to access specific values.Let us see how series data structure can be index to get value from a specific index.Example Live Demoimport pandas as pd my_data = [34, 56, 78, 90, 123, 45] my_index = ['ab', 'mn' ,'gh', 'kl', 'wq', 'az'] my_series = pd.Series(my_data, index = my_index) print("The series contains following elements") print(my_series) print("The second element (zero-based indexing)") print(my_series[2]) print("Elements from 2 to the last element are") print(my_series[2:])OutputThe series contains following elements ab  34 mn  56 gh  78 kl  90 wq ... Read More

Explain how series data structure in Python can be created using dictionary and explicit index values?

AmitDiwan
Updated on 10-Dec-2020 13:37:38

176 Views

Let us understand how series data structure can be created using dictionary, as well as specifying the index values, i.e., customized index values to the series.Dictionary is a Python data structure that has a mapping kind of structure- a key, value pair.Example Live Demoimport pandas as pd my_data = {'ab' : 11., 'mn' : 15., 'gh' : 28., 'kl' : 45.} my_index = ['ab', 'mn' ,'gh', 'kl'] my_series = pd.Series(my_data, index = my_index) print("This is series data structure created using dictionary and specifying index values") print(my_series)OutputThis is series data structure created using dictionary and specifying index values ab  11.0 mn  15.0 ... Read More

How can SciPy be used to calculate the cube root of values and exponential values in Python?

AmitDiwan
Updated on 10-Dec-2020 13:36:14

566 Views

When it is required to find the cube root of values, a function present in SciPy library can be used.Syntax of ‘cbrt’ functionscipy.special.cbrt(x)The ‘x’ is the parameter that is passed to the function ‘cbrt’ that is present in ‘special’ class of ‘SciPy’ library. Here’s an example −Example Live Demofrom scipy.special import cbrt my_cb = cbrt([27, 89]) print("The cube roots are :") print(my_cb)OutputThe cube roots are : [3. 4.4647451]ExplanationThe required packages are imported.The ‘cbrt’ function is called on the list of values whose cube root needs to be computed.The output is displayed on the console.When it is required to find the 10**x ... Read More

How can scikit learn library be used to preprocess data in Python?

AmitDiwan
Updated on 10-Dec-2020 13:34:59

320 Views

Pre-processing data refers to cleaning of data, removing invalid data, noise, replacing data with relevant values and so on.This doesn’t always mean text data; it could also be images or video processing as well. It is an important step in the machine learning pipeline.Data pre-processing basically refers to the task of gathering all the data (which is collected from various resources or a single resource) into a common format or into uniform datasets (depending on the type of data).This is done so that the learning algorithm can learn from this dataset and give relevant results with high accuracy. Since real-world ... Read More

How to apply functions element-wise in a dataframe in Python?

AmitDiwan
Updated on 10-Dec-2020 13:30:21

1K+ Views

It may sometimes be required to apply certain functions along the elements of the dataframe. All the functions can’t be vectorised. This is where the function ‘applymap’ comes into picture.This takes a single value as input and returns a single value as output.Example Live Demoimport pandas as pd import numpy as np my_df = pd.DataFrame(np.random.randn(5, 5), columns=['col_1', 'col_2', 'col_3', 'col_4', 'col_5']) print("The dataframe generated is ") print(my_df) my_df.applymap(lambda x:x*11.45) print("Using the applymap function") print(my_df.apply(np.mean))OutputThe dataframe generated is     col_1       col_2      col_3      col_4     col_5 0  -0.671510  -0.860741   0.886484   0.842158   ... Read More

How can a specific operation be applied row wise or column wise in Pandas Python?

AmitDiwan
Updated on 10-Dec-2020 13:28:19

399 Views

It may sometimes be required to apply certain functions along the axes of a dataframe. The axis can be specified, otherwise the default axis is considered as column-wise, where every column is considered as an array.If the axis is specified, then the operations are performed row-wise on the data.The ‘apply’ function can be used in conjunction with the dot operator on the dataframe. Let us see an example −Example Live Demoimport pandas as pd import numpy as np my_data = {'Age':pd.Series([45, 67, 89, 12, 23]), 'value':pd.Series([8.79, 23.24, 31.98, 78.56, 90.20])} print("The dataframe is :") my_df = pd.DataFrame(my_data) print(my_df) print("The description of ... Read More

How can data be summarized in Pandas Python?

AmitDiwan
Updated on 10-Dec-2020 13:27:07

137 Views

Lots of information about the data can be obtained by using different functions on it. But if we wish to get all information on the data, the ‘describe’ function can be used.This function will give information such as ‘count’, ‘mean’, ‘standard deviation’, the 25th percentile, the 50th percentile, and the 75th percentile.Example Live Demoimport pandas as pd my_data = {'Name':pd.Series(['Tom', 'Jane', 'Vin', 'Eve', 'Will']), 'Age':pd.Series([45, 67, 89, 12, 23]), 'value':pd.Series([8.79, 23.24, 31.98, 78.56, 90.20]) } print("The dataframe is :") my_df = pd.DataFrame(my_data) print(my_df) print("The description of data is :") print(my_df.describe())OutputThe dataframe is :    Name  Age   value 0  Tom   ... Read More

How to find the standard deviation of specific columns in a dataframe in Pandas Python?

AmitDiwan
Updated on 10-Dec-2020 13:25:13

7K+ Views

Standard deviation tells about how the values in the dataset are spread. They also tells how far the values in the dataset are from the arithmetic mean of the columns in the dataset.Sometimes, it may be required to get the standard deviation of a specific column that is numeric in nature. This is where the std() function can be used. The column whose mean needs to be computed can be indexed to the dataframe, and the mean function can be called on this using the dot operator.The index of the column can also be passed to find the standard deviation.Let ... Read More

How to get the mean of a specific column in a dataframe in Python?

AmitDiwan
Updated on 10-Dec-2020 13:23:58

2K+ Views

Sometimes, it may be required to get the mean value of a specific column that is numeric in nature. This is where the ‘mean’ function can be used.The column whose mean needs to be computed can be indexed to the dataframe, and the mean function can be called on this using the dot operator.The index of the column can also be passed to find the mean. The term mean() refers to finding the sum of all values and dividing it by the total number of values in the dataset.Let us see a demonstration of the same −Example Live Demoimport pandas as ... Read More

How can decision tree be used to construct a classifier in Python?

AmitDiwan
Updated on 10-Dec-2020 13:20:24

184 Views

Decision tree is the basic building block of the random forest algorithm. It is considered as one of the most popular algorithms in machine learning and is used for classification purposes. They are extremely popular because they are easy to understand.The decision given out by a decision tree can be used to explain why a certain prediction was made. This means the in and out of the process would be clear to the user.They are also a foundation for ensemble methods such as bagging, random forests, and gradient boosting. They are also known as CART, i.e. Classification And Regression Trees. ... Read More

Advertisements