Python for Business Analyst

Pandas Example – Write a Pandas program to remove the time zone information from a Time series data

Hits: 7 (Python Example for Beginners)   Write a Pandas program to remove the time zone information from a Time series data.   Sample Solution: Python Code : import pandas as pd date1 = pd.Timestamp(‘2019-01-01′, tz=’Europe/Berlin’) date2 = pd.Timestamp(‘2019-01-01′, tz=’US/Pacific’) date3 = pd.Timestamp(‘2019-01-01′, tz=’US/Eastern’) print(“Time series data with time zone:”) print(date1) print(date2) print(date3) print(“nTime series …

Pandas Example – Write a Pandas program to create a time series object with a time zone

Hits: 3 (Python Example for Beginners)   Write a Pandas program to create a time series object with a time zone.   Sample Solution: Python Code : import pandas as pd print(“Timezone: Europe/Berlin:”) print(“Using pytz:”) date_pytz = pd.Timestamp(‘2019-01-01’, tz = ‘Europe/Berlin’) print(date_pytz.tz) print(“Using dateutil:”) date_util = pd.Timestamp(‘2019-01-01’, tz = ‘dateutil/Europe/Berlin’) print(date_util.tz) print(“nUS/Pacific:”) print(“Using pytz:”) date_pytz …

Pandas Example – Write a Pandas program to check if a day is a business day (weekday) or not

Hits: 3 (Python Example for Beginners)   Write a Pandas program to check if a day is a business day (weekday) or not.   Sample Solution: Python Code : import pandas as pd def is_business_day(date): return bool(len(pd.bdate_range(date, date))) print(“Check busines day or not?”) print(‘2020-12-01: ‘,is_business_day(‘2020-12-01’)) print(‘2020-12-06: ‘,is_business_day(‘2020-12-06’)) print(‘2020-12-07: ‘,is_business_day(‘2020-12-07’)) print(‘2020-12-08: ‘,is_business_day(‘2020-12-08’)) Sample Output: Check busines …

Pandas Example – Write a Pandas program to convert year and day of year into a single datetime column of a dataframe

Hits: 3 (Python Example for Beginners)   Write a Pandas program to convert year and day of year into a single datetime column of a dataframe.   Sample Solution: Python Code : import pandas as pd data = { “year”: [2002, 2003, 2015, 2018], “day_of_the_year”: [250, 365, 1, 140] } df = pd.DataFrame(data) print(“Original DataFrame:”) …

Pandas Example – Write a Pandas program to create a time series using three months frequency

Hits: 4 (Python Example for Beginners)   Write a Pandas program to create a time series using three months frequency.   Sample Solution: Python Code : import pandas as pd time_series = pd.date_range(‘1/1/2021′, periods = 36, freq=’3M’) print(“Time series using three months frequency:”) print(time_series) Sample Output: Time series using three months frequency: DatetimeIndex([‘2021-01-31’, ‘2021-04-30’, ‘2021-07-31’, …

Pandas Example – Write a Pandas program to create a time series object that has time indexed data

Hits: 3 (Python Example for Beginners)   Write a Pandas program to create a time series object that has time indexed data. Also select the dates of same year and select the dates between certain dates.   Sample Solution: Python Code : import pandas as pd index = pd.DatetimeIndex([‘2011-09-02’, ‘2012-08-04’, ‘2015-09-03’, ‘2010-08-04’, ‘2015-03-03’, ‘2011-08-04’, ‘2015-04-03’, …

Pandas Example – Write a Pandas program to create a time-series with two index labels and random values

Hits: 3 (Python Example for Beginners)   Write a Pandas program to create a time-series with two index labels and random values. Also print the type of the index.   Sample Solution: Python Code : import pandas as pd import numpy as np import datetime from datetime import datetime, date dates = [datetime(2011, 9, 1), …

Pandas Example – Write a Pandas program to print the day after and before a specified date

Hits: 1 (Python Example for Beginners)   Write a Pandas program to print the day after and before a specified date. Also print the days between two given dates.   Sample Solution: Python Code : import pandas as pd import datetime from datetime import datetime, date today = datetime(2012, 10, 30) print(“Current date:”, today) tomorrow …

Pandas Example – Write a Pandas program to create a date from a given year, month, day and another date from a given string formats

Hits: 6 (Python Example for Beginners)   Write a Pandas program to create a date from a given year, month, day and another date from a given string formats.   Sample Solution: Python Code : from datetime import datetime date1 = datetime(year=2020, month=12, day=25) print(“Date from a given year, month, day:”) print(date1) from dateutil import …

Pandas Example – Write a Pandas program to create Time Series Objects

Hits: 4 (Python Example for Beginners)   Write a Pandas program to create a) Datetime object for Jan 15 2012. b) Specific date and time of 9:20 pm. c) Local date and time. d) A date without time. e) Current date. f) Time from a datetime. g) Current local time.   Sample Solution: Python Code …

Pandas Example – Write a Pandas program to split a given dataset using group by on multiple columns and drop last n rows of from each group

Hits: 3 (Python Example for Beginners)   Write a Pandas program to split a given dataset using group by on multiple columns and drop last n rows of from each group.   Test Data: ord_no purch_amt ord_date customer_id salesman_id 0 70001 150.50 2012-10-05 3002 5002 1 70009 270.65 2012-09-10 3001 5003 2 70002 65.26 2012-10-05 …

Pandas Example – Write a Pandas program to split the following dataset using group by on first column and aggregate over multiple lists on second column

Hits: 0 (Python Example for Beginners)   Write a Pandas program to split the following dataset using group by on first column and aggregate over multiple lists on second column.   Test Data: student_id marks 0 S001 [88, 89, 90] 1 S001 [78, 81, 60] 2 S002 [84, 83, 91] 3 S002 [84, 88, 91] …

Pandas Example – Write a Pandas program to split a given dataset using group by on specified column into two labels and ranges

Hits: 6 (Python Example for Beginners)   Write a Pandas program to split a given dataset using group by on specified column into two labels and ranges. Split the group on ‘salesman_id’, Ranges: 1) (5001…5006) 2) (5007..5012)   Test Data: salesman_id sale_jan 0 5001 150.50 1 5002 270.65 2 5003 65.26 3 5004 110.50 4 …

Pandas Example – Write a Pandas program to split a given dataset, group by one column and remove those groups if all the values of a specific columns are not available

Hits: 3 (Python Example for Beginners)   Write a Pandas program to split a given dataset, group by one column and remove those groups if all the values of a specific columns are not available. Test Data: school class name date_Of_Birth age height weight address S1 s001 V Alberto Franco 15/05/2002 12 173 35 street1 …

Pandas Example – Write a Pandas program to split a given dataset, group by one column and apply an aggregate function to few columns and another aggregate function to the rest of the columns of the dataframe

Hits: 10 (Python Example for Beginners)   Write a Pandas program to split a given dataset, group by one column and apply an aggregate function to few columns and another aggregate function to the rest of the columns of the dataframe.   Test Data: salesman_id sale_jan sale_feb sale_mar sale_apr sale_may sale_jun 0 5002 150.50 250.50 …

Pandas Example – Write a Pandas program to split a given dataset, group by two columns and convert other columns of the dataframe into a dictionary with column header as key

Hits: 4 (Python Example for Beginners)   Write a Pandas program to split a given dataset, group by two columns and convert other columns of the dataframe into a dictionary with column header as key. Test Data: school class name date_Of_Birth age height weight address S1 s001 V Alberto Franco 15/05/2002 12 173 35 street1 …