Found 507 Articles for Pandas

Finding the length of words in a given Pandas series

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

794 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 frequency of each item in a Pandas series?

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

369 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

973 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

Comparing two Pandas series and printing the the difference

Prasad Naik
Updated on 16-Mar-2021 10:49:44

2K+ Views

In this program, we will compare two Pandas series and will print the differences in the series. By difference, we mean that the index positions at which the elements did not match.AlgorithmStep 1: Define two Pandas series, s1 and s2. Step 2: Compare the series using compare() function in the Pandas series. Step 3: Print their difference.Example Codeimport pandas as pd s1 = pd.Series([10, 20, 30, 40, 50, 60]) s2 = pd.Series([10, 30, 30, 40, 55, 60]) print("S1:", s1) print("S2:", s2) difference = s1.compare(s2) print("Difference between the series: ", difference)OutputS1: 0    10 1    20 2 ... Read More

Print the standard deviation of Pandas series

Prasad Naik
Updated on 16-Mar-2021 10:48:04

300 Views

In this program, we will find the standard deviation of a Pandas series. Standard deviation is a statistic that measures the dispersion of a dataset relative to its mean and is calculated as the square root of the variance.AlgorithmStep 1: Define a Pandas series Step 2: Calculate the standard deviation of the series using the std() function in the pandas library. Step 3: Print the standard deviation.Example Codeimport pandas as pd series = pd.Series([10,20,30,40,50]) print("Series: ", series) series_std = series.std() print("Standard Deviation of the series: ",series.std())OutputSeries: 0    10 1    20 2    30 3    40 4    50 dtype: int64 Standard Deviation of the series:  15.811388300841896

Print the mean of a Pandas series

Prasad Naik
Updated on 16-Mar-2021 10:47:48

951 Views

mean() function in the Pandas library can be used to find the mean of a series.AlgorithmStep 1: Define a Pandas series. Step 2: Use the mean() function to calculate the mean. Step 3: Print the mean.Example Codeimport pandas as pd series = pd.Series([10,20,30,40,50]) print("Pandas Series: ", series) series_mean = series.mean() print("Mean of the Pandas series:", series_mean)OutputPandas Series: 0    10 1    20 2    30 3    40 4    50 dtype: int64 Mean of the Pandas series: 30.0

How to append elements to a Pandas series?

Prasad Naik
Updated on 16-Mar-2021 10:43:12

15K+ Views

In this program, we will append elements to a Pandas series. We will use the append() function for this task. Please note that we can only append a series or list/tuple of series to the existing series.AlgorithmStep1: Define a Pandas series, s1. Step 2: Define another series, s2. Step 3: Append s2 to s1. Step 4: Print the final appended series.Example Codeimport pandas as pd s1 = pd.Series([10, 20, 30, 40, 50]) s2 = pd.Series([11, 22, 33, 44, 55]) print("S1:", s1) print("S2:", s2) appended_series = s1.append(s2) print("Final Series after appending:", appended_series)OutputS1: 0    10 1    20 ... Read More

Pandas timeseries plot setting X-axis major and minor ticks and labels

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

295 Views

Using Pandas, we can create a dataframe with time and speed, and thereafter, we can use the data frame to get the desired plot.StepsConstruct a new Generator with the default BitGenerator (PCG64).Using Pandas, get a fixed frequency DatetimeIndex. From '2020-01-01' to '2021-01-01'.Draw samples from a log-normal distribution.Make a data frame with above data.Using panda dataframe create plot, with figsize = (10, 5).To show the figure, use the plt.show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt rng = np.random.default_rng(seed=1) date_day = pd.date_range(start='2020-01-01', end='2021-01-01', freq='D') traffic = rng.lognormal(sigma=2, size=date_day.size) df_day = pd.DataFrame(dict(speed=[pow(2, -i) for ... Read More

How to sort a Pandas Series?

Prasad Naik
Updated on 16-Mar-2021 10:42:52

234 Views

In this problem we have to sort a Pandas series. We will define an unsorted pandas series and will sort it using the sort_values() function in the Pandas library.AlgorithmStep 1: Define Pandas series. Step 2: Sort the series using sort_values() function. Step 3: Print the sorted series.Example Codeimport pandas as pd panda_series = pd.Series([18, 15, 66, 92, 55, 989]) print("Unsorted Pandas Series: ", panda_series) panda_series_sorted = panda_series.sort_values(ascending = True) print("Sorted Pandas Series: ", panda_series_sorted)OutputUnsorted Pandas Series: 0     18 1     15 2     66 3     92 4     55 5   ... Read More

How to give a Pandas/Matplotlib bar graph custom colors?

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

4K+ Views

To make a custom color, we can create a hexadecimal string. From it, we can make different sets of color representation and can pass them into the scatter method to get the desired output.Using the set_color method, we could set the color of the bar.StepsTake user input for the number of bars.Add bar using plt.bar() method.Create colors from hexadecimal alphabets by choosing random characters.Set the color for every bar, using set_color() method.To show the figure we can use plt.show() method.Examplefrom matplotlib import pyplot as plt import random bar_count = int(input("Enter number of bars: ")) bars = plt.bar([i for ... Read More

Advertisements