Pradeep Elance

Pradeep Elance

302 Articles Published

Articles by Pradeep Elance

Page 11 of 31

A += B Assignment Riddle in Python

Pradeep Elance
Pradeep Elance
Updated on 03-Jul-2020 166 Views

In this chapter we see what happens when we update the values in a tuple, which is actually immutable. We will be able to merge new values with old values but that throws an error. We can study the bytecode of the error and understand better how the rules for tuple work.First we define a tuple and then issue a command to update its last element as shown below.Example>>> tupl = (5, 7, 9, [1, 4]) >>> tupl[3] += [6, 8]OutputRunning the above code gives us the following result −Traceback (most recent call last): File "", line 1, in TypeError: ...

Read More

Absolute Deviation and Absolute Mean Deviation using NumPy

Pradeep Elance
Pradeep Elance
Updated on 03-Jul-2020 1K+ Views

In Statistical analysis study of data variability in a sample indicates how dispersed are the values in a given data sample. The two important ways we calculate the variability are Absolute Deviation and  Mean Absolute Deviation.Absolute DeviationIn this method we first find the mean value of the given sample and then calculate the difference between each value and the mean value of the sample called as the absolute deviation value of each data sample. So for the values higher than mean the value the deviation value will be positive and for those lower than the mean value the deviation value ...

Read More

Absolute and Relative frequency in Pandas

Pradeep Elance
Pradeep Elance
Updated on 03-Jul-2020 901 Views

In statistics, the term "frequency" indicates the number of occurrences of a value in a given data sample. As a software meant for mathematical and scientific analysis, Pandas has many in-built methods to calculate frequency from a given sample.Absolute Frequency It is same as just the frequency where the number of occurrences of a data element is calculated. In the below example, we simply count the number of times the name of a city is appearing in a given DataFrame and report it out as frequency.Approach 1 − We use the pandas method named .value_counts.Exampleimport pandas as pd # Create ...

Read More

Accessing elements of a Pandas Series

Pradeep Elance
Pradeep Elance
Updated on 30-Jun-2020 11K+ Views

Pandas series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The elements of a pandas series can be accessed using various methods.Let's first create a pandas series and then access it's elements.Creating Pandas SeriesA panadas series is created by supplying data in various forms like ndarray, list, constants and the index values which must be unique and hashable. An example is given below.Exampleimport pandas as pd s = pd.Series([11, 8, 6, 14, 25], index = ['a', 'b', 'c', 'd', 'e']) print sOutputRunning the above code gives us the following result ...

Read More

Add new column in Pandas Data Frame Using a Dictionary

Pradeep Elance
Pradeep Elance
Updated on 30-Jun-2020 1K+ Views

Pandas Data Frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. It can be created using python dict, list, and series etc. In this article, we will see how to add a new column to an existing data frame.So first let's create a data frame using pandas series. In the below example we are converting a pandas series to a Data Frame of one column, giving it a column name Month_no.Exampleimport pandas as pd s = pd.Series([6, 8, 3, 1, 12]) df = pd.DataFrame(s, columns=['Month_No']) print (df)OutputRunning the above code gives ...

Read More

a.sort, sorted(a), np_argsort(a) and np.lexsort(b, a) in Python

Pradeep Elance
Pradeep Elance
Updated on 30-Jun-2020 768 Views

Ordering of data elements in a specific order is a frequently needed operation. To sort elements in an array, python uses the functions named sorted() and array.sort().sorted(array)This function returns a sorted array without modifying the original array.a = [9, 5, 3, 1, 12, 6] b = sorted([9, 5, 3, 1, 12, 6]) print "Sorted Array :", print (b) print "Original Array :", print (a)Running the above code gives us the following result −Sorted Array : [1, 3, 5, 6, 9, 12] Original Array : [9, 5, 3, 1, 12, 6]list.sort()The sort function returns a sorted array by doing in-place modification ...

Read More

Get match indices in Python

Pradeep Elance
Pradeep Elance
Updated on 04-Jun-2020 642 Views

Two lists are given. We need to find the index of the elements from the first list whose values match with the elements in the second list.With indexWe simply design follow to get the value of the element in the second list and extract the corresponding index from the first list.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = ['Tue', 'Fri'] # Given lists print("The given list: ", listA) print("The list of values: ", listB) # using indices res = [listA.index(i) for i in listB] # Result print("The Match indices list is : ", res)OutputRunning the above code gives ...

Read More

Get last N elements from given list in Python

Pradeep Elance
Pradeep Elance
Updated on 04-Jun-2020 1K+ Views

Given a Python list we want to find out only e e the last few elements.With slicingThe number of positions to be extracted is given. Based on that we design is slicing technique to slice elements from the end of the list by using a negative sign.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Given list print("Given list : ", listA) # initializing N n = 4 # using list slicing res = listA[-n:] # print result print("The last 4 elements of the list are : ", res)OutputRunning the above code gives us the following result −Given list ...

Read More

Get key with maximum value in Dictionary in Python

Pradeep Elance
Pradeep Elance
Updated on 04-Jun-2020 843 Views

A Python dictionary contains key value pairs. In this article we will see how to get the key of the element whose value is maximum in the given Python dictionary.With max and getWe use the get function and the max function to get the key.Example Live DemodictA = {"Mon": 3, "Tue": 11, "Wed": 8} print("Given Dictionary:", dictA) # Using max and get MaxKey = max(dictA, key=dictA.get) print("The Key with max value:", MaxKey)OutputRunning the above code gives us the following result −Given Dictionary: {'Mon': 3, 'Tue': 11, 'Wed': 8} The Key with max value: TueWith itemgetter and maxWith itemgetter function we get ...

Read More

Get indices of True values in a binary list in Python

Pradeep Elance
Pradeep Elance
Updated on 04-Jun-2020 856 Views

When a Python list contains values like true or false and 0 or 1 it is called binary list. In this article we will take a binary list and find out the index of the positions where the list element is true.With enumerateThe enumerate function extracts all the elements form the list. We apply a in condition to check of the extracted value is true or not.Example Live DemolistA = [True, False, 1, False, 0, True] # printing original list print("The original list is : ", listA) # using enumerate() res = [i for i, val in enumerate(listA) if val] # ...

Read More
Showing 101–110 of 302 articles
« Prev 1 9 10 11 12 13 31 Next »
Advertisements