Python for Business Analyst

Pandas Example – Write a Pandas program to get the powers of an array values element-wise

(Python Example for Beginners)   Write a Pandas program to get the powers of an array values element-wise. Note: First array elements raised to powers from second array Sample data: {‘X’:[78,85,96,80,86], ‘Y’:[84,94,89,83,86],’Z’:[86,97,96,72,83]}   Sample Solution : Python Code : import pandas as pd df = pd.DataFrame({‘X’:[78,85,96,80,86], ‘Y’:[84,94,89,83,86],’Z’:[86,97,96,72,83]}); print(df) Sample Output: X Y Z 0 78 84 …

Pandas Example – Write a Pandas program to check the equality of two given series

(Python Example for Beginners)   Write a Pandas program to check the equality of two given series.   Sample Solution : Python Code : import pandas as pd nums1 = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1]) nums2 = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1]) print(“Original Series:”) print(nums1) print(nums2) …

Pandas Example – Write a Pandas program to create a TimeSeries to display all the Sundays of given year.

(Python Example for Beginners)   Write a Pandas program to create a TimeSeries to display all the Sundays of given year.   Sample Solution : Python Code : import pandas as pd result = pd.Series(pd.date_range(‘2020-01-01′, periods=52, freq=’W-SUN’)) print(“All Sundays of 2019:”) print(result) Sample Output: All Sundays of 2019: 0 2020-01-05 1 2020-01-12 2 2020-01-19 3 2020-01-26 …

Pandas Example – Write a Pandas program to compute the Euclidean distance between two given series

(Python Example for Beginners)   Write a Pandas program to compute the Euclidean distance between two given series.   Euclidean distance From Wikipedia, In mathematics, the Euclidean distance or Euclidean metric is the “ordinary” straight-line distance between two points in Euclidean space. With this distance, Euclidean space becomes a metric space. The associated norm is called …

Pandas Example – Write a Pandas program to filter words from a given series that contain atleast two vowels

(Python Example for Beginners)   Write a Pandas program to filter words from a given series that contain atleast two vowels.   Sample Solution : Python Code : import pandas as pd from collections import Counter color_series = pd.Series([‘Red’, ‘Green’, ‘Orange’, ‘Pink’, ‘Yellow’, ‘White’]) print(“Original Series:”) print(color_series) print(“nFiltered words:”) result = mask = color_series.map(lambda c: sum([Counter(c.lower()).get(i, …

Pandas Example – Write a Pandas program to convert a series of date strings to a timeseries.

(Python Example for Beginners)   Write a Pandas program to convert a series of date strings to a timeseries.   Sample Solution : Python Code : import pandas as pd date_series = pd.Series([’01 Jan 2015′, ’10-02-2016′, ‘20180307’, ‘2014/05/06’, ‘2016-04-12’, ‘2019-04-06T11:20’]) print(“Original Series:”) print(date_series) print(“nSeries of date strings to a timeseries:”) print(pd.to_datetime(date_series)) Sample Output: Original Series: 0 …

Pandas Example – Write a Pandas program to compute difference of differences between consecutive numbers of a given series

(Python Example for Beginners)   Write a Pandas program to compute difference of differences between consecutive numbers of a given series.   Sample Solution : Python Code : import pandas as pd series1 = pd.Series([1, 3, 5, 8, 10, 11, 15]) print(“Original Series:”) print(series1) print(“nDifference of differences between consecutive numbers of the said series:”) print(series1.diff().tolist()) print(series1.diff().diff().tolist()) …

Pandas Example – Write a Pandas program to create a subset of a given series based on value and condition

(Python Example for Beginners)   Write a Pandas program to create a subset of a given series based on value and condition.   Sample Solution : Python Code : import pandas as pd s = pd.Series([0, 1,2,3,4,5,6,7,8,9,10]) print(“Original Data Series:”) print(s) print(“nSubset of the above Data Series:”) n = 6 new_s = s[s < n] print(new_s) …

Pandas Example – Write a Pandas program to sort a given Series

(Python Example for Beginners)   Write a Pandas program to sort a given Series.   Sample Solution : Python Code : import pandas as pd s = pd.Series([‘100’, ‘200’, ‘python’, ‘300.12’, ‘400’]) print(“Original Data Series:”) print(s) new_s = pd.Series(s).sort_values() print(new_s) Sample Output: Original Data Series: 0 100 1 200 2 python 3 300.12 4 400 dtype: …

Pandas Example – Write a Pandas program to compare the elements of the two Pandas Series

(Python Example for Beginners)   Write a Pandas program to compare the elements of the two Pandas Series. Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 10]   Sample Solution: Python Code : import pandas as pd ds1 = pd.Series([2, 4, 6, 8, 10]) ds2 = pd.Series([1, 3, 5, 7, 10]) print(“Series1:”) …