Found 10784 Articles for Python

Python - Filter even values from a list

Naveen Singh
Updated on 07-Jan-2020 06:46:30

1K+ Views

As part of data analysis require to filter out values from a list meeting certain criteria. In this article we'll see how to filter out only the even values from a list.We have to go through each element of the list and divide it with 2 to check for the remainder. If the remainder is zero then we consider it as an even number. After fetching these even numbers from a list we will put a condition to create a new list which excludes this even numbers. That new list is the result of the filtering condition we applied.Using for ... Read More

Multiplication of two Matrices in Single line using Numpy in Python

Naveen Singh
Updated on 07-Jan-2020 06:41:16

555 Views

Matrix multiplication is a lengthy process where each element from each row and column of the matrixes are to be multiplied and added in a certain way. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result matrix has the number of rows of the first and the number of columns of the second matrix.For smaller matrices we may design nested for loops and find the result. For bigger matrices we need some built in functionality in python to tackle this. We will see both ... Read More

All possible permutations of N lists in Python

Naveen Singh
Updated on 07-Jan-2020 06:34:45

777 Views

If we have two lists and we need to combine each element of the first element with each element of the second list, then we have the below approaches.Using For LoopIn this straight forward approach we create a list of lists containing the permutation of elements from each list. we design a for loop within another for loop. The inner for loop refers to the second list and Outer follow refers to the first list.Example Live DemoA = [5, 8] B = [10, 15, 20] print ("The given lists : ", A, B) permutations = [[m, n] for m in ... Read More

Add leading Zeros to Python string

Naveen Singh
Updated on 18-Feb-2020 11:22:50

485 Views

We may sometimes need to append zeros as string to various data elements in python. There may the reason for formatting and nice representation or there may be the reason for some calculations where these values will act as input. Below are the methods which we will use for this purpose.Using format()Here we take a DataFrame and apply the format function to the column wher we need to append the zeros as strings. The lambda method is used to apply the function repeatedly.Example Live Demoimport pandas as pd string = {'Column' : ['HOPE', 'FOR', 'THE', 'BEST']} dataframe=pd.DataFrame(string) print("given column is ") ... Read More

Python - Column summation of tuples

Naveen Singh
Updated on 02-Jan-2020 10:26:34

197 Views

Python has extensive availability of various libraries and functions which make it so popular for data analysis. We may get a need to sum the values in a single column for a group of tuples for our analysis. So in this program we are adding all the values present at the same position or same column in a series of tuples.It can be achieved in the following ways.Using for loop and zipUsing the for loop we loop through each item and apply zip function to gather the values from each column. Then we apply the sum function and finally get ... Read More

Python - Change column names and row indexes in Pandas DataFrame

Naveen Singh
Updated on 02-Jan-2020 10:23:35

2K+ Views

Pandas is a python library offering many features for data analysis which is not available in python standard library. One such feature is the use of Data Frames. They are rectangular grids representing columns and rows. While creating a Data frame, we decide on the names of the columns and refer them in subsequent data manipulation. But there may be a situation when we need to change the name of the columns after the data frame has been created. In this article, we will see how to achieve that.Using rename()This is the most preferred method as we can change both ... Read More

max() and min() in Python

Naveen Singh
Updated on 02-Jan-2020 10:16:18

2K+ Views

Finding maximum and minimum values from a given list of values is a very common need in data processing programs. Python has these two functions which handle both numbers and strings. We will see both the scenarios in the below examples.Numeric ValuesWe take a list of numeric values which has integers and floats. The functions work appropriately to give both the max and the min values.Example Live Demox=[10, 15, 25.5, 3, 2, 9/5, 40, 70] print("Maximum number is :", max(x)) print("Minimum number is :", min(x))OutputRunning the above code gives us the following result:Maximum number is : 70 Minimum number is : ... Read More

Getter and Setter in Python

Naveen Singh
Updated on 02-Jan-2020 10:13:52

11K+ Views

For the purpose of data encapsulation, most object oriented languages use getters and setters method. This is because we want to hide the attributes of a object class from other classes so that no accidental modification of the data happens by methods in other classes.As the name suggests, getters are the methods which help access the private attributes or get the value of the private attributes and setters are the methods which help change or set the value of private attributes.Accessing Private AttributeBelow we write code to create a class, initialize it and access it variables without creating any additional ... Read More

Fractal Trees in Python

Naveen Singh
Updated on 02-Jan-2020 10:07:38

719 Views

Fractal patterns are all around us in nature. Like a small branch taken out of the leaf of a fern leaf resembles the leaf itself. Or a pebble often resembles the shape of a mountain! So this idea of a repetition of small pattern to generate a large pattern is known as a fractal tree. In python programming we can also generate fractal trees by using various modules available.Using pygame ModuleThis module provides us with the required functions to generate the fractal trees. Here we first defines the screen layout size and then define the deepness up to which the ... Read More

Decision tree implementation using Python

Naveen Singh
Updated on 02-Jan-2020 10:01:51

1K+ Views

Decision tree is an algorithm which is mainly applied to data classification scenarios. It is a tree structure where each node represents the features and each edge represents the decision taken. Starting from the root node we go on evaluating the features for classification and take a decision to follow a specific edge. Whenever a new data point comes in , this same method is applied again and again and then the final conclusion is taken when all the required features are studied or applied to the classification scenario. So Decision tree algorithm is a supervised learning model used in ... Read More

Advertisements