Python Tutorials

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 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 …

How to plot learning curve of an xgboost model

Hits: 10 (How to plot learning curve of an xgboost model) In this Learn through Codes example, you will learn how to plot learning curve of an xgboost model.    Python Example for Beginners Free Machine Learning & Data Science Coding Tutorials in Python & R for Beginners. Subscribe @ Western Australian Center for …

Pandas Example – Write a Pandas program to split a given dataframe into groups with multiple aggregations

Hits: 6 (Python Example for Beginners)   Write a Pandas program to split a given dataframe into groups with multiple aggregations. Split the following given dataframe by school code, class and get mean, min, and max value of height and age for each value of the school. Test Data: school class name date_Of_Birth age height …

Pandas Example – Write a Pandas program to merge two given datasets using multiple join keys

Hits: 3 (Python Example for Beginners)   Write a Pandas program to merge two given datasets using multiple join keys.   Test Data: data1: key1 key2 P Q 0 K0 K0 P0 Q0 1 K0 K1 P1 Q1 2 K1 K0 P2 Q2 3 K2 K1 P3 Q3 data2: key1 key2 R S 0 K0 …

Pandas Example – Write a Pandas program to join two dataframes using keys from right dataframe only

Hits: 1 (Python Example for Beginners)   Write a Pandas program to join two dataframes using keys from right dataframe only.   Test Data: data1: key1 key2 P Q 0 K0 K0 P0 Q0 1 K0 K1 P1 Q1 2 K1 K0 P2 Q2 3 K2 K1 P3 Q3 data2: key1 key2 R S 0 …

Pandas Example – Write a Pandas program to join the two given dataframes along rows and merge with another dataframe along the common column id

Hits: 7 (Python Example for Beginners)   Write a Pandas program to join the two given dataframes along rows and merge with another dataframe along the common column id.   Test Data: student_data1: student_id name marks 0 S1 Danniella Fenton 200 1 S2 Ryder Storey 210 2 S3 Bryce Jensen 190 3 S4 Ed Bernal …

Python Example – Write a NumPy program to find a matrix or vector norm

Hits: 6 (Python Example for Beginners)   Write a NumPy program to find a matrix or vector norm. Notes on Vector and Matrix Norms from here.   Sample Solution : Python Code : import numpy as np v = np.arange(7) result = np.linalg.norm(v) print(“Vector norm:”) print(result) m = np.matrix(‘1, 2; 3, 4’) result1 = np.linalg.norm(m) print(“Matrix norm:”) …

Python Example – Write a Python program to test whether all numbers of a list is greater than a certain number

Hits: 7 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to test whether all numbers of a list is greater than a certain number.   Sample Solution Python Code: num = [2,3,4] print() print(all(x > 1 for x in num)) print(all(x > 4 for x in num)) print() Sample …

Python Examples for Beginners: Write a Python program to test whether the system is a big-endian platform or little-endian platform

Hits: 11 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to test whether the system is a big-endian platform or little-endian platform. Sample Solution: Python Code: import sys print() if sys.byteorder == “little”: print(“Little-endian platform.”) else: print(“Big-endian platform.”) print() Sample Output: Little-endian platform   Write a Python program to …

Python Examples for Beginners: Write a Python program to get the command-line arguments (name of the script, the number of arguments, arguments) passed to a script

Hits: 8 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the command-line arguments (name of the script, the number of arguments, arguments) passed to a script. Sample Solution Python Code (test.py): import sys print(“This is the name/path of the script:”),sys.argv[0] print(“Number of arguments:”,len(sys.argv)) print(“Argument List:”,str(sys.argv)) The command …

Python Examples for Beginners: Write a Python program to calculate midpoints of a line

Hits: 7 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate midpoints of a line.   Sample Solution Python Code: print(‘nCalculate the midpoint of a line :’) x1 = float(input(‘The value of x (the first endpoint) ‘)) y1 = float(input(‘The value of y (the first endpoint) ‘)) x2 …

Python Examples for Beginners: Python Code to Add Two Numbers

Hits: 4 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort files by date. Sample Solution:- Python Code: import glob import os files = glob.glob(“*.txt”) files.sort(key=os.path.getmtime) print(“n”.join(files)) Sample Output: result.txt temp.txt myfile.txt mynewtest.txt mytest.txt abc.txt test.txt   Python Examples for Beginners: Python Code to Add Two Numbers Free …

Python Examples for Beginners: Write a Python program to sort three integers without using conditional statements and loops

Hits: 21 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort three integers without using conditional statements and loops.   Sample Solution Python Code: x = int(input(“Input first number: “)) y = int(input(“Input second number: “)) z = int(input(“Input third number: “)) a1 = min(x, y, z) a3 …