To set the border color of the dots in matplotlib scatterplots, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable "N" to store the number of sample data.Create x and y data points using numpy.Plot the x and y data points using scatter() method. To set the border color of the dots, use the edgecolors parameter in the scatter() method. Here, we have used "red" as the border color of the dots by using edgecolors='red'.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot ... Read More
To determine the order of bars in a bar chart in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Add a subplot to the current figure.Make a bar plot with dataframe, df.Add a subplot to the current figure.Create another dataframe, df_sorted, by column marks.Make a bar plot with df_sorted.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame( ... Read More
To reverse the column order, use the dataframe.columns and set as -1 −dataFrame[dataFrame.columns[::-1]At first, import the required library −import pandas as pd Create a DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] })Reverse the column order −df = dataFrame[dataFrame.columns[::-1]] ExampleFollowing is the code −import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, ... Read More
To remove a column with all null values, use the dropna() method and set the “how” parameter to “all” −how='all'At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a DataFrame. We have set the NaN values using the Numpy np.infdataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": [np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN] } )To remove a column with all null values, use dropna() and set the required parameters −dataFrame.dropna(how='all', axis=1, inplace=True) ExampleFollowing is the code ... Read More
To fetch only capital words, we are using regex. The re module is used here and imported. Let us import all the libraries −import re import pandas as pdCreate a DataFrame −data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'], ['KEYBOARD', 'charger', 'SMARTTV', 'camera']] dataFrame = pd.DataFrame(data)Now, extract capital words −for i in range(dataFrame.shape[1]): for ele in dataFrame[i]: if bool(re.match(r'\w*[A-Z]\w*', str(ele))): print(ele)ExampleFollowing is the code −import re import pandas as pd # create a dataframe data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'], ... Read More
Use the isin() method to display True for infinite values. At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a dictionary of list. We have set the infinity values using the Numpy np.inf −d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] } Creating DataFrame from the above dictionary of list −dataFrame = pd.DataFrame(d)Display True for infinite values −res = dataFrame.isin([np.inf, -np.inf]) ExampleFollowing is the code −import pandas as pd import numpy as np # dictionary of list d = { "Reg_Price": [7000.5057, np.inf, 5000, ... Read More
To check and display row index, use the isinf() with any(). At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a dictionary of list. We have set the infinity values using the Numpy np.inf −d = { "Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf] } Creating DataFrame from the above dictionary of list −dataFrame = pd.DataFrame(d)Getting row index with infinity values −indexNum = dataFrame.index[np.isinf(dataFrame).any(1)] ExampleFollowing is the code −import pandas as pd import numpy as np # dictionary of list d = { "Reg_Price": [7000.5057, np.inf, ... Read More
To count the observations, first use the groupby() and then use count() on the result. At first, import the required library −dataFrame = pd.DataFrame({'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, 25, 50]})Group the column with duplicate values −group = dataFrame.groupby("Product Category") Get the count −group.count()ExampleFollowing is the code −import pandas as pd # create a dataframe dataFrame = pd.DataFrame({'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, ... Read More
To sort data in ascending or descending order, use sort_values() method. For ascending order, use the following is the sort_values() method −ascending=TrueImport the required library −import pandas as pd Create a DataFrame with 3 columns −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 2000], "Place": ['Pune', 'Delhi', 'Mumbai', 'Hyderabad', 'Bangalore', 'Chandigarh'] } ) To sort DataFrame in ascending order according to the element frequency, we need to count the occurrences. Therefore, count() is also used with sort_values() set for asscending order sort −dataFrame.groupby(['Car'])['Reg_Price'].count().reset_index(name='Count').sort_values(['Count'], ... Read More
To get the maximum of column values, use the max() function. At first, import the required Pandas library −import pandas as pdNow, create a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Finding the maximum value of a single column “Units” using max() −print"Maximum Units from DataFrame1 = ", dataFrame1['Units'].max()In the same way, we have calculated the maximum value from the 2nd DataFrame.ExampleFollowing is the complete code −import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP