Found 33676 Articles for Programming

Finding the length of words in a given Pandas series

Prasad Naik
Updated on 16-Mar-2021 11:02:49

955 Views

In this task, we will find the length of strings in a Pandas series. We will use the str.len() function in the Pandas library for this purpose.AlgorithmStep 1: Define a Pandas series of string. Step 2: Find the length of each string using the str.len() function. Step 3: Print the results.Example Codeimport pandas as pd series = pd.Series(["Foo", "bar", "London", "Quarantine"]) print("Series: ", series) length = series.str.len() print("Length:", length)OutputSeries: 0           Foo 1           bar 2        London 3    Quarantine dtype: object Length: 0     3 1     3 2     6 3    10 dtype: int64

How to calculate the z score for grouped data in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:56:35

643 Views

To calculate the z score for grouped data, we can use ave function and scale function. For example, if we have a data frame called df that contains a grouping coloumn say GROUP and a numerical column say Response then we can use the below command to calculate the z score for this data −ave(df$Response,df$GROUP,FUN=scale)ExampleConsider the below data frame − Live Demogrp

Setting Different Bar color in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:56:01

17K+ Views

We can first create bars and then, by using set_color, we can set the color of the bars.StepsPass two lists consisting of four elements, into the bars method argument.Step 1 returns bars.Return values (0, 1, 2, 3) can be set with different colors, using set_color() method. Green, Black, Red color will be set and one bar will have the default color.To show the figure, use plt.show() method.Examplefrom matplotlib import pyplot as plt bars = plt.bar([1, 2, 3, 4], [1, 2, 3, 4]) bars[0].set_color('green') bars[1].set_color('black') bars[2].set_color('red') plt.show()Output

How to set the current figure in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:54:16

973 Views

Using the figure() method, we can set the current figure.StepsCreate a new figure, or activate an existing figure, with the window title “Welcome to figure 1”.Create a new figure, or activate an existing figure, with the window title “Welcome to figure 2”.Using plt.show(), show the figures.Examplefrom matplotlib import pyplot as plt plt.figure("Welcome to figure 1") plt.figure("Welcome to figure 2")    # Active Figure plt.show()Output

How to animate a scatter plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:54:57

6K+ Views

Using the FuncAnimation method of matplotlib, we can animate the diagram. We can pass a user defined method where we will be changing the position of the particles, and at the end, we will return plot type.StepsGet the particle's initial position, velocity, force, and size.Create a new figure, or activate an existing figure with figsize = (7, 7).Add an axes to the current figure and make it the current axes, with xlim and ylim.Plot scatter for initial position of the particles.Make an animation by repeatedly calling a function *func*. We can pass a user-defined method that helps to change the ... Read More

Finding the multiples of a number in a given list using NumPy

Prasad Naik
Updated on 16-Mar-2021 10:54:33

2K+ Views

In this program, we will find the index position at which a multiple of a given number exists. We will use both the Numpy and the Pandas library for this task.AlgorithmStep 1: Define a Pandas series. Step 2: Input a number n from the user. Step 3: Find the multiples of that number from the series using argwhere() function in the numpy library.Example Codeimport numpy as np listnum = np.arange(1, 20) multiples = [] print("NumList:", listnum) n = int(input("Enter the number you want to find multiples of: ")) for num in listnum:    if num % n == ... Read More

How to calculate the frequency of each item in a Pandas series?

Prasad Naik
Updated on 16-Mar-2021 10:55:14

487 Views

In this program, we will calculate the frequency of each element in a Pandas series. The function value_counts() in the pandas library helps us to find the frequency of elements.AlgorithmStep 1: Define a Pandas series. Step 2: Print the frequency of each item using the value_counts() function.Example Codeimport pandas as pd series = pd.Series([10,10,20,30,40,30,50,10,60,50,50]) print("Series:", series) frequency = series.value_counts() print("Frequency of elements:", frequency)OutputSeries: 0     10 1     10 2     20 3     30 4     40 5     30 6     50 7     10 8     60 9     50 10    50 dtype: int64 Frequency of elements: 50    3 10    3 30    2 20    1 40    1 60    1 dtype: int64

How to get the nth percentile of a Pandas series?

Prasad Naik
Updated on 16-Mar-2021 10:55:31

1K+ Views

A percentile is a term used in statistics to express how a score compares to other scores in the same set. In this program, we have to find nth percentile of a Pandas series.AlgorithmStep 1: Define a Pandas series. Step 2: Input percentile value. Step 3: Calculate the percentile. Step 4: Print the percentile.Example Codeimport pandas as pd series = pd.Series([10, 20, 30, 40, 50]) print("Series:", series) n = int(input("Enter the percentile you want to calculate: ")) n = n/100 percentile = series.quantile(n) print("The {} percentile of the given series is: {}".format(n*100, percentile))OutputSeries: 0    10 1 ... Read More

Python xticks in subplots

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:50:15

5K+ Views

Subplot can split the figure in nrow*ncols parts and plt.xticks could help to plot the xticks for subplots.StepsCreate two lists for line 1 and line 2.Add a subplot to the current figure, nrow = 1, ncols = 2 and index = 1.Draw line 1 with style as dashed.Set or retrieve auto scaling margins(0.2).Place xticks at even places.Set a title for the X-axis.Add a subplot to the current figure, nrow = 1, ncols = 2 and index = 2.Plot line 2.Set or retrieve auto scaling margins(0.2).Place xticks at odd places.Set a title for the X-axis.To show the figure use plt.show() method.Exampleimport ... Read More

How to remove lines in a Matplotlib plot?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:50:44

9K+ Views

We will create two lines, i.e., line1 and line2. After that, we will pop the second line and will remove it.StepsCreate lists for line1 and line2.Plot line1 and line 2 using plot() method, line 2 with style =’dashed’.Set or retrieve auto-scaling margins(0.2).Pop line 2, and remove it using remove() method.The final figure will have only one line, so use plt.show() method.Exampleimport matplotlib.pyplot as plt line1 = [2, 4, 8] line2 = [3, 6, 12] plt.plot(line1) line_2 = plt.plot(line2, linestyle='dashed') plt.margins(0.2) plt.title("With extra lines") plt.show() plt.plot(line1) l = line_2.pop(0) l.remove() plt.margins(0.2) plt.title("Removed extra lines") plt.show()Input Diagram (Before ... Read More

Advertisements