Sometimes during our data analysis, we need to look at the duplicate rows to understand more about our data rather than dropping them straight away.Luckily, in pandas we have few methods to play with the duplicates..duplciated()This method allows us to extract duplicate rows in a DataFrame. We will use a new dataset with duplicates. I have downloaded the Hr Dataset from link.import pandas as pd import numpy as np # Import HR Dataset with certain columns df = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/HRDataset.csv", usecols = ("Employee_Name""PerformanceScore", "Position", "CitizenDesc")) #Sort the values on employee name and make it permanent df.sort_values("Employee_Name"inplace = True) df.head(3)Employee_NamePositionCitizenDescPerformanceScore0AdinolfiProduction ... Read More
Suppose we have a list of flights as [origin, destination] pairs. The list is shuffled; we have to find all the airports that were visited in the correct order. If there are more than one valid itinerary, return lexicographically smallest ones first.So, if the input is like flights = [["Mumbai", "Kolkata"], ["Delhi", "Mumbai"], ["Kolkata", "Delhi"] ], then the output will be ['Delhi', 'Mumbai', 'Kolkata', 'Delhi']To solve this, we will follow these stepsins := an empty mapouts := an empty mapadj_list := an empty mapDefine a function dfs() . This will take airportwhile outs[airport] is not null, donxt := size of ... Read More
IntroductionPandas have a dual selection capability to select the subset of data using the Index position or by using the Index labels. Inthis post, I will show you how to "Select a Subset Of Data Using lexicographical slicing".Google is full of datasets. Search for movies dataset in kaggle.com. This post uses the movies data set from kaggle.How to do it1. Import the movies dataset with only the columns required for this example.import pandas as pd import numpy as np movies = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv", index_col="title", usecols=["title", "budget", "vote_average", "vote_count"]) movies.sample(n=5)titlebudgetvote_averagevote_countLittle Voice06.661Grown Ups 2800000005.81155The Best Years of Our Lives21000007.6143Tusk28000005.1366Operation Chromite05.8292. I always recommend ... Read More
Suppose we have a string s and another number k, we have to check whether we can create k palindromes using all characters in s or not.So, if the input is like s = "amledavmel" k = 2, then the output will be True, as we can make "level" and "madam".To solve this, we will follow these stepsd := a map where store each unique characters and their frequencycnt := 0for each key in d, doif d[key] is odd, thencnt := cnt + 1if cnt > k, thenreturn Falsereturn TrueLet us see the following implementation to get better understandingExamplefrom collections ... Read More
In this post, I will show you how to create charts in excel using Python - Openpyxl module. We will create an excel spreadsheet from scratch with Tennis players grandslam titles as the data for creating bar charts using the openpyxl module.Introduction..Microsoft office has started providing a new extension to Microsoft Excel sheets, which is .xlsx, from Office 2007 to support storing more rows and columns.This change had moved Excel sheets to a XML based file format with ZIP compression. The world is ruled by Microsoft spreadsheets, people have been using spreadsheets for various purposes and one of the use ... Read More
Suppose we have a number n; we have to find the minimum number of Fibonacci numbers required to add up to n.So, if the input is like n = 20, then the output will be 3, as We can use the Fibonacci numbers [2, 5, 13] to sum to 20.To solve this, we will follow these stepsres := 0fibo := a list with values [1, 1]while last element of fibo n, dodelete last element from fibon := n - last element of fibores := res + 1return resLet us see the following implementation to get better understandingExampleclass Solution: ... Read More
Suppose we have a number n, we have to find the number of trailing zeros of n!.So, if the input is like n = 20, then the output will be 4, as 20! = 2432902008176640000To solve this, we will follow these stepsset count := 0for i := 5, (n/i) > 1, update i := i * 5, docount := count + (n /i)return countLet us see the following implementation to get better understandingExample Live Demo#include #include #define MAX 20 using namespace std; int countTrailingZeros(int n) { int count = 0; for (int i = 5; n / i >= 1; i *= 5) count += n / i; return count; } main() { int n = 20; cout
Suppose we have a 2D matrix representing an excel spreadsheet. We have to find the same matrix with all cells and formulas computed. An excel spreadsheet looks like belowB17035=A1+A2The columns are named as (A, B, C...) and rows are (1, 2, 3....) Each cell will either contain a value, a reference to another cell, or an excel formula for an operation with between numbers or cell reference. (Example. "=A1+5", "=A2+B2", or "=2+5")So, if the input is likeB17035=A1+A2then the output will be7703510as the B1 = 7 (The first row second column) and "=A1 + A2" is 7 + 3 = 10.To ... Read More
We can interpolate data values into strings using various formats. We can use this to dedug code, produce reports, forms, and other outputs. In this topic, We will see three ways of formatting strings and how to interpolate data values into strings.Python has three ways of formatting strings:% - old school (supported in Python 2 and 3)() - new style (Python 2.6 and up){} - f-strings (Python 3.6 and up)Old style: %The old style of string formatting has the form format_string % data. The format strings are nothing but interpolation sequences.syntaxdescription%sstring%ddecimal%xhexa-int%ooctal-int%fdecimal-float%eexponential-float%gdecimal or exponential-float%%literal %# printing integer with % style type. ... Read More
Given a list of integers nums, you can perform the following operation: pick the largest number in nums and turn it into the second largest number. Return the minimum number of operations required to make all integers the same in the list.So, if the input is like nums = [5, 9, 2], then the output will be 3, as pick 9 first, then make it 5, so array is [5, 5, 2], then pick 5 and make 2, [5, 2, 2], again pick 5 and convert into 2, [2, 2, 2].To solve this, we will follow these stepsvals := sort ... Read More