Found 10476 Articles for Python

Comparing two Pandas series and printing the the difference

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

3K+ 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

377 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

1K+ 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

16K+ 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

452 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

329 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

Python program to reverse a Numpy array?

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

614 Views

This is a simple program wherein we have to reverse a numpy array. We will use numpy.flip() function for the same.AlgorithmStep 1: Import numpy. Step 2: Define a numpy array using numpy.array(). Step 3: Reverse the array using numpy.flip() function. Step 4: Print the array.Example Codeimport numpy as np arr = np.array([10,20,30,40,50]) print("Original Array: ", arr) arr_reversed = np.flip(arr) print("Reversed Array: ", arr_reversed)OutputOriginal Array: [10 20 30 40 50] Reversed Array: [50 40 30 20 10]

How to print array elements within a given range using Numpy?

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

838 Views

In this program, we have to print elements of a numpy array in a given range. The different numpy functions used are numpy.where() and numpy.logical_and().AlgorithmStep 1: Define a numpy array. Step 2: Use np.where() and np.logical_and() to find the numbers within the given range. Step 3: Print the result.Example Codeimport numpy as np arr = np.array([1,3,5,7,10,2,4,6,8,10,36]) print("Original Array:",arr) result = np.where(np.logical_and(arr>=4, arr

How to find set difference between two Numpy arrays?

Prasad Naik
Updated on 16-Mar-2021 10:41:10

11K+ Views

In this program, we will find the set difference of two numpy arrays. We will use the setdiff1d() function in the numpy library. This function takes two parameters: array1 and array2 and returns the unique values in array1 that are not in array2.AlgorithmStep 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find the set difference between these arrays using the setdiff1d() function. Step 4: Print the output.Example Codeimport numpy as np array_1 = np.array([2, 4, 6, 8, 10, 12]) print("Array 1: ", array_1) array_2 = np.array([4, 8, 12]) print("Array 2: ", array_2) set_diff = ... Read More

How to find intersection between two Numpy arrays?

Prasad Naik
Updated on 16-Mar-2021 10:37:11

3K+ Views

In this problem, we will find the intersection between two numpy arrays. Intersection of two arrays is an array with elements common in both the original arraysAlgorithmStep 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find intersection between the arrays using the numpy.intersect1d() function. Step 4: Print the array of intersecting elements.Example Codeimport numpy as np array_1 = np.array([1,2,3,4,5]) print("Array 1:", array_1) array_2 = np.array([2,4,6,8,10]) print("Array 2:", array_2) intersection = np.intersect1d(array_1, array_2) print("The intersection between the two arrays is:", intersection)OutputArray 1:  [1 2 3 4 5] Array 2:  [2  4  6  8 10] The intersection between the two arrays is:  [2 4]

Advertisements