We can use pandas.DataFrame.corr to compute pairwise correlation of columns, excluding NULL values. The correlation coefficient indicates the strength of the linear association between two variables. The coefficient ranges between -1 and 1.To get the correlation between two numeric columns in a Pandas dataframe, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe of two-dimensional, size-mutable, potentially heterogeneous tabular data.Compare the values of the two columns and compute the correlation coefficient using col1.corr(col2).Print the correlation coefficient on the console.To display the figure, use show() method.Exampleimport pandas as ... Read More
The Finite Element Method (FEM) is used in a variety of tasks such as modeling of different material types, testing complex geometries, visualizing the local effects acting on a small area of a design. It basically breaks a large spatial domain into simple parts called "finite elements". The simple equations that model these finite elements are then collected into a larger system of equations to model the entire domain.To plot 2d FEM results using matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create nodes, elements and node values data ... Read More
To add a legend with vertical line in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Plot the vertical line with red color.The line can have both a solid linestyle connecting all the vertices, and a marker at each vertex.Place a legend on the plot with vertical line.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from matplotlib import lines plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() color = 'red' ax.plot([0, 0], [0, 3], ... Read More
To exponentially scale the Y-axis with matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Inintialize a variable dt for steps.Create x and y data points using numpy.Plot the x and y data points using numpy.Set the exponential scale for the Y-axis, using plt.yscale('symlog').To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True dt = 0.01 x = np.arange(-50.0, 50.0, dt) y = np.arange(0, 100.0, dt) plt.plot(x, y) plt.yscale('symlog') plt.show()OutputIt will produce the following ... Read More
To annotate Seaborn pairplots, we can use the fig.text() method.StepsImport Seaborn, Pandas, Numpy, and Pyplot packages.Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe of two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot pairwise relationships in a dataset, using sns.pairplot().Add an annotated text using fig.text() method.To display the figure, use show() method.Exampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame( np.random.random((4, 4)), columns=["a", "b", "c", "d"] ) pp = ... Read More
To rename multiple column headers, use the rename() method and set the dictionary in the columns parameter. At first, let us create a DataFrame −dataFrame = pd.DataFrame({"Car": ['BMW', 'Mustang', 'Tesla', 'Mustang', 'Mercedes', 'Tesla', 'Audi'], "Cubic Capacity": [2000, 1800, 1500, 2500, 2200, 3000, 2000], "Reg Price": [7000, 1500, 5000, 8000, 9000, 6000, 1500], "Units Sold": [ 200, 120, 150, 120, 210, 250, 220] })Creating a dictionary to rename columns. The key and value pairs as old name and new name −dictionary = {'Car': 'Car Name', 'Cubic Capacity': 'CC', 'Reg Price': 'Registration Price', 'Units Sold': 'Units Purchased' }Use rename() and set the ... Read More
To filter rows based on column values, we can use the query() function. In the function, set the condition through which you want to filter records. At first, import the required library −import pandas as pdFollowing is our data with Team Records −Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50], ['Bangladesh', 6, 40]]Create a DataFrame from above and add columns as well −dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points']) Use query() to filter records with “Rank” equal to 5 −dataFrame.query("Rank == 5"))ExampleFollowing is the complete code −import pandas as ... Read More
When it is required to convert a list of list to a list of set, the ‘map’, ‘set’, and ‘list’ methods are used.ExampleBelow is a demonstration of the samemy_list = [[2, 2, 2, 2], [1, 2, 1], [1, 2, 3], [1, 1], [0]] print("The list of lists is: ") print(my_list) my_result = list(map(set, my_list)) print("The resultant list is: ") print(my_result)OutputThe list of lists is: [[2, 2, 2, 2], [1, 2, 1], [1, 2, 3], [1, 1], [0]] The resultant list is: [set([2]), set([1, 2]), set([1, 2, 3]), set([1]), set([0])]ExplanationA list of list is defined and is displayed ... Read More
When it is required to get all the subset having a specific sum ‘s’, a method is defined that iterates through the list and gets all combinations of the list, and if it matches the sum, it is printed on the console.ExampleBelow is a demonstration of the samefrom itertools import combinations def sub_set_sum(size, my_array, sub_set_sum): for i in range(size+1): for my_sub_set in combinations(my_array, i): if sum(my_sub_set) == sub_set_sum: print(list(my_sub_set)) my_size = 6 my_list = [21, 32, 56, 78, 45, 99, 0] ... Read More
When it is required to find all the strings that are substrings of a given list of strings, the ‘set’ and ‘list’ attributes are used.ExampleBelow is a demonstration of the samemy_list_1 = ["Hi", "there", "how", "are", "you"] my_list_2 = ["Hi", "there", "how", "have", "you", 'been'] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = list(set([elem_1 for subset_1 in my_list_1 for elem_1 in my_list_2 if elem_1 in subset_1])) print("The result is :") print(my_result)OutputThe first list is : ['Hi', 'there', 'how', 'are', 'you'] The second list is : ['Hi', 'there', 'how', 'have', 'you', 'been'] The ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP