Category Archives: Python

Introduction to Pandas (Python data analysis toolkit)

Pandas is one of the most useful data analysis library in Python. It aims to be the fundamental high-level building block for doing practical, real world data analysis in Python.

pandas is well suited for many different kinds of data:

  • Tabular data with heterogeneously-typed columns, as in an SQL table or Excel spreadsheet
  • Ordered and unordered (not necessarily fixed-frequency) time series data.
  • Arbitrary matrix data with row and column labels (homogeneously typed or heterogeneous)
  • Any other form of observational / statistical data sets.

Lets understand the 2 key data structures in Pandas – Series and DataFrames

Introduction to Series and Dataframes

Series can be understood as a 1 dimensional labelled / indexed array. You can access individual elements of this series through these labels.

A dataframe is similar to Excel workbook – you have column names referring to columns and you have rows, which can be accessed with use of row numbers. The essential difference being that column names and row numbers are known as column and row index, in case of dataframes.

Series and dataframes form the core data model for Pandas in Python. The data sets are first read into these dataframes and then various operations (e.g. group by, aggregation etc.) can be applied very easily to its columns.

Here are just a few of the things that pandas does well:

  • Easy handling of missing data (represented as NaN) in floating point as well as non-floating point data
  • Size mutability: columns can be inserted and deleted from DataFrame and higher dimensional objects
  • Automatic and explicit data alignment: objects can be explicitly aligned to a set of labels, or the user can simply ignore the labels and let Series, DataFrame, etc. automatically align the data for you in computations
  • Powerful, flexible group by functionality to perform split-apply-combine operations on data sets, for both aggregating and transforming data
  • Make it easy to convert ragged, differently-indexed data in other Python and NumPy data structures into DataFrame objects
  • Intelligent label-based slicing, fancy indexing, and subsetting of large data sets
  • Intuitive merging and joining data sets
  • Flexible reshaping and pivoting of data sets
  • Hierarchical labeling of axes (possible to have multiple labels per tick)
  • Robust IO tools for loading data from flat files (CSV and delimited), Excel files, databases, and saving / loading data from the ultrafast HDF5 format
  • Time series-specific functionality: date range generation and frequency conversion, moving window statistics, moving window linear regressions, date shifting and lagging, etc.



Find sum of series 1+2+8+26+80+…….+n

we can not define 1+2+8+26+80+………. series in a simple manner to write  program.


2,8,26,80,242……………..can be define as below

The way to discover the next term is multiply the next number by 2 and then add it on.

i.e. the next number of 2 is 3

so, 3*2=6+2=8


27*2=54+26=80…….. and so on


Far more simply multiply the number by 3 and add 2


2 * 3 + 2 = 8;

8 * 3 + 2 = 28

26 *3 + 2 = 80

80 *3 + 2 = 242. and so on


Now for finding the sum of the given series and add 1 with sum of this  series.

C language implementation


// C program to add 1+2+8+26+80+…….+n
//Author: Sani Kamal
//Date: 04-Oct-17


int main(){

int n,temp=0,i,sum=0,hold=0;
 printf("How many term");
 temp =3*hold+2;
 sum +=hold;

Java language implementation

import java.util.Scanner;

 * Java program to add 1+2+8+26+80+.......+n
 * @author Sani Kamal
public class SeriesSum {

public static void main(String[] args) {
 Scanner input = new Scanner(;
 System.out.print("How many term?");
 int n = input.nextInt();
 int temp, hold = 0, sum = 0;
 for (int i = 0; i < n - 1; i++) {
 temp = 3 * hold + 2;
 hold = temp;
 sum += hold;

 System.out.println("Sum=" + (sum+1));




Python language implementation

# Python program to add 1+2+8+26+80+.......+n
# Author: Sani Kamal
# Date: 04-Oct-17

num = int(input("How many term?"))
hold = 0
for i in range(0, num - 1):
    temp = 3 * hold + 2
    hold = temp
    sum += hold
print(sum + 1)

Polar Coordinates

You are given a complex z. Your task is to convert it to polar coordinates.

Input Format

A single line containing the complex number z.

Output Format

Output two lines:
The first line should contain the value of r.
The second line should contain the value of Φ.

Sample Input


Sample Output


import cmath

Set .add()


Apply your knowledge of the .add() operation to help your friend Rupal.

Rupal has a huge collection of country stamps. She decided to count the total number of distinct country stamps in her collection. She asked for your help. You pick the stamps one by one from a stack of N country stamps.

Find the total number of distinct country stamps.

Input Format

The first line contains an integer N, the total number of country stamps.
The next N lines contains the name of the country where the stamp is from.


num = int(input())
country_name = set()
for i in range(num):

List Comprehensions

Let’s learn about list comprehensions! You are given three integers X,Y and Z representing the dimensions of a cuboid along with an integer N. You have to print a list of all possible coordinates given by  (i,j,k) on a 3D grid where the sum of  i+j+k  is not equal to N . Here, 0<=i<=X;0<=j<=Y;0<=k<=Z

Input Format

Four integers  X,Y,Z and N  each on four separate lines, respectively.


Print the list in lexicographic increasing order.

x,y,z,n=[int(input())for i in range(4)]
p=[[a,b,c]for a in range(x+1)for b in range(y+1)for c in range(z+1) if a+b+c!=n]