Programming Articles - Page 1039 of 3363

Program to find shortest subarray to be removed to make array sorted in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:12:57

309 Views

Suppose we have an array called arr, we have to remove a subarray of arr such that the remaining elements in arr are in non-decreasing order. We have to find the length of the shortest subarray to remove.So, if the input is like arr = [10, 20, 30, 100, 40, 20, 30, 50], then the output will be 3 because we can remove [100, 40, 20] which is smallest subarray of length 3, and by removing these all are in non-decreasing order [10, 20, 30, 30, 50].To solve this, we will follow these steps:n := size of arrarr := insert ... Read More

Program to find number of ways to split a string in Python

Arnab Chakraborty
Updated on 04-Oct-2021 08:02:11

438 Views

Suppose we have a binary string s we can split s into 3 non-empty strings s1, s2, s3 such that (s1 concatenate s2 concatenate s3 = s). We have to find the number of ways s can be split such that the number of characters '1' is the same in s1, s2, and s3. The answer may be very large so return answer mod 10^9+7.So, if the input is like s = "11101011", then the output will be 2 because we can split them like "11 | 1010 | 11" and "11 | 101 | 011".To solve this, we will ... Read More

Python - Density Plots with Pandas for a specific attribute

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

629 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

Python Pandas - Draw a vertical violinplot grouped by a categorical variable with Seaborn

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

584 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

Python Pandas - Draw a bar plot and set a cap to the error bars with Seaborn

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

941 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

Python Pandas - Group the swarms by a categorical variable with Seaborn

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

153 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

Python - Draw a single horizontal swarm plot with Seaborn

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

243 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

Python Pandas - Draw a violin plot, explicit order and show observation as a stick with 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

Python Pandas- Create multiple CSV files from existing CSV file

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

Program to find maximum length of subarray with positive product in Python

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

301 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

Advertisements