Find Maximum Length of Subarray with Positive Product in Python

Arnab Chakraborty
Updated on 04-Oct-2021 07:47:16

287 Views

Suppose we have an array called nums, we have to find the maximum length of a subarray where the product of all its elements is positive. We have to find the maximum length of a subarray with positive product.So, if the input is like nums = [2, -2, -4, 5, -3], then the output will be 4 because first four elements are forming a subarray whose product is positive.To solve this, we will follow these steps :Define a function util() . This will take s, eneg := 0ns := -1, ne := -1for i in range s to e, doif ... Read More

Density Plots with Pandas for a Specific Attribute

AmitDiwan
Updated on 04-Oct-2021 07:42:33

613 Views

We will use plot.density() to density plot on a Dataset in the form of a csv file. Let’s say the following is our dataset − Cricketers2.csvAt first, import the required libraries −import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Plotting the density plot. Attribute considered is "Age" −dataFrame.Age.plot.density(color='green')ExampleFollowing is the complete code −import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") # plotting the density plot # attribute considered is "Age" dataFrame.Age.plot.density(color='green') plt.title('Density ... Read More

Draw Vertical Violinplot Grouped by Categorical Variable with Seaborn

AmitDiwan
Updated on 04-Oct-2021 07:39:55

572 Views

Violin Plot in Seaborn is used to draw a combination of boxplot and kernel density estimate. The seaborn.violinplot() is used for this. We will plotti violin plot with the columns grouped by a categorical variable.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") Plotting violin plot with Role and Age grouped by a categorical variable −sb.violinplot(x = 'Role', y = "Age", data = dataFrame)ExampleFollowing ... Read More

Draw a Bar Plot and Set Error Bars with Seaborn in Python Pandas

AmitDiwan
Updated on 04-Oct-2021 07:37:07

930 Views

Bar Plot in Seaborn is used to show point estimates and confidence intervals as rectangular bars. The seaborn.barplot() is used. Set caps to the error bars using the capsize parameter.Let’s say the following is our dataset in the form of a CSV file − Cricketers2.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Set caps to the error bars using the capsize parameter −sb.barplot(x=dataFrame["Role"], y=dataFrame["Matches"], capsize=.3)ExampleFollowing is the code −import seaborn as sb import pandas as pd import matplotlib.pyplot ... Read More

Group the Swarms by a Categorical Variable with Seaborn in Python Pandas

AmitDiwan
Updated on 04-Oct-2021 07:35:05

145 Views

Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this. Group the swarms by a categorical variable by simply setting it as one of the x and y coordinates.Let’s say the following is our dataset in the form of a CSV file − Cricketers2.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Group the swarms by a categorical variable −sb.swarmplot(x = dataFrame["Role"], y = dataFrame["Age"])ExampleFollowing is the code −import ... Read More

Draw a Single Horizontal Swarm Plot with Seaborn

AmitDiwan
Updated on 04-Oct-2021 07:32:45

228 Views

Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this. Let’s say the following is our dataset in the form of a CSV file − Cricketers2.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Plotting swarm plot with the “Matches” column −sb.swarmplot(x = dataFrame["Matches"])ExampleFollowing is the code −import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into a ... Read More

Draw a Violin Plot with Explicit Order and Show Observations as Sticks using Seaborn

AmitDiwan
Updated on 04-Oct-2021 07:29:39

2K+ Views

Violin Plot in Seaborn is used to draw a combination of boxplot and kernel density estimate. The seaborn.violinplot() is used for this. Observations show as a stick using the inner parameter with value stick.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") Plotting violin plot with Academy and Age. Control order by passing an explicit order i.e. ordering on the basis of "Academy". Observations ... Read More

Create Multiple CSV Files from Existing CSV File in Python Pandas

AmitDiwan
Updated on 04-Oct-2021 07:26:20

1K+ Views

Let’s say the following is our CSV file −SalesRecords.csvAnd we need to generate 3 excel files from the above existing CSV file. The 3 CSV files should be on the basis of the Car names i.e. BMW.csv, Lexus.csv and Jaguar.csv.At first, read our input CSV file i.e. SalesRecord.csv −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv")Use groupby() to generate CSVs on the basis of Car names in Car column −for (car), group in dataFrame.groupby(['Car']): group.to_csv(f'{car}.csv', index=False)ExampleFollowing is the code −import pandas as pd # DataFrame to read our input CS file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") print("Input CSV file = ", dataFrame) ... Read More

Write Pandas DataFrame to CSV File

AmitDiwan
Updated on 04-Oct-2021 07:16:22

5K+ Views

To write pandas dataframe to a CSV file in Python, use the to_csv() method. At first, let us create a dictionary of lists −# dictionary of lists d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22'] }Now, create pandas dataframe from the above dictionary of lists −dataFrame = pd.DataFrame(d) Our output CSV file will generate on the Desktop since we have set the Desktop path below −dataFrame.to_csv("C:\Users\amit_\Desktop\sales1.csv\SalesRecords.csv")ExampleFollowing is the code −import pandas as pd # dictionary of lists d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', ... Read More

Find Greatest Subarray of Given Length in Python

Arnab Chakraborty
Updated on 04-Oct-2021 07:07:58

678 Views

Suppose we have an array containing various integer values and a given length k. We have to find out the greatest subarray from the array of the given length. A subarray is said to be greater than another subarray, if subarray1[i] ≠ subarry2[i] and subarray1[i] > subarry2[i].So, if the input is like nums = [5, 3, 7, 9], k = 2, then the output will be [7, 9].To solve this, we will follow these steps −start := size of nums - kmax_element := nums[start]max_index := startwhile start >= 0, doif nums[start] > max_element is non-zero, thenmax_element := nums[start]max_index := startreturn ... Read More

Advertisements