Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, 4 January 2017

Learning Pandas #4 - Hierarchical Indexing

Sunday, 1 January 2017

Learning Pandas #3 - Working on Summary & MissingData

Saturday, 31 December 2016

Learning Pandas - DataFrame #2

Friday, 30 December 2016

Learning Pandas - Series #1

Monday, 12 September 2016

Python Points #15 - Exceptions

Tuesday, 14 June 2016

Python Points #14 - Code a childhood game

Level : Intermediate

Try to code this famous childhood game played in india, known as "Raja, Mantri, Chor, Sipahi", in python by seeing the game output shared below -

Little bit about Game:
Chits are made for Raja/King(100 points), Mantri/Minister(80 points),Chor/Thief(0 points) and Sipahi/Insprector(50 points). These chits are then thrown in the middle and 4 players pick one each. Raja/King then exclaims ‘Mera Manrti kaun?’ (Who is my minister?){In my game/code, King is so smart and asked directly to Mantri/Minister} Mantri/Minister responds and s/he is then asked to identify the Chor/Thief (Who stole my Queen's neckless ). If he guesses correctly then the points are retained if s/he is incorrect that he has to surrender the points to the Chor/Thief. The player with highest point wins in the end.

Like the below page to get update

Tuesday, 31 May 2016

Python Points #13 - Comprehensions

Sunday, 29 May 2016

Python Points - Assignments

Friday, 1 April 2016

jupyter notebook tip #1

How we can start jupyter notebook from a specific directory?

Below, I am sharing a very tiny batch (and linux shell) script which will kick off the jupyter notebook from a particular directory with just a double click...

I hope, this will give you a little ease while working on IPython :-)

Like the below page to get update

Friday, 11 March 2016

Python Points #12 - execute OS commads

Thursday, 10 March 2016

Python Points #11 - Set

Tuesday, 8 March 2016

Python - Get line no with print statement

Sometime when we are working on some code and there are lot of print statement, It's difficult to check for which print statement produce this statement.

It helps a lot while debugging the code and helped me to create better python learning script which will generate the output with the code line no which is easy to relate.

from inspect import currentframe

def lno():
    cf = currentframe()
    val = str(cf.f_back.f_lineno)+". "
    return val

print "this is Me", lno()
print lno(), "Hi! there"


this is Me  8.
9.  Hi! there

** This is a little overhead on the code as with each print statement the function is being called. So use it wisely :-)

Like the below page to get update

Sunday, 6 March 2016

Python SyntaxError - Non-ASCII character '\xe2' in file

If you get below error while running your python code - 

SyntaxError: Non-ASCII character '\xe2' in file .\ on line 32, but no encoding declared; see for details

and You are using Notepad++ so here is how you have to resolve this -

1. By converting the Text Encoding

Go to Menu -> Encoding -> Convert to UTF-8

and save the file.

2. By seach and replace the \xe2 value to empty

Use Ctrl-F
Find [^\xe2]+
or Find [^\x00-\x7F]+ to delete all non-ascii char
Select Search mode as -Regular Expression
Hit Enter to replace all values

3. In Linux

a. Find the line which is having bad charaters -
grep -nP "[\x80-\xFF]" INPUT_FILE

b. Some ways to remove 
sed -i 's/[^[:print:]]//g' INPUT_FILE > clean-file
sed 's/[\x80-\xff]//g' INPUT_FILE > clean-file
tr -cd '\11\12\15\40-\176' < INPUT_FILE > clean-file

** word of caution - It may remove some charaters which you need file as we are using range, so take a backup of your file first

Like the below page to get update

Monday, 22 February 2016

Python Points #10d - Writing into Files

Wednesday, 17 February 2016

Python Points #10c - File Methods

Sunday, 7 February 2016

Python Points #10b - Reading Files

Saturday, 6 February 2016

Python Points #10a - Reading Files

Wednesday, 3 February 2016

Python Points #9 - Tuples

Wednesday, 27 January 2016

Python Points #8 - Dictionary

Wednesday, 20 January 2016

Import your own python library

How to use your own written functions or routines when code in python??

Follow below steps for auto import - 
1. Create a folder where you can put all reusable code/function/routines files
2. Let's say it is "routines"
3. Now suppose, you have written all your functions and save it in a file and saved it in routines folder
4. Add routines folder path to your windows path or linux path
for linux:
Edit your .bash_profile (typically towards the end)to the following line
export PYTHON_PATH=$PYTHON_PATH:'/path/to/folder/'   #if you have this variable
export PATH=$PATH:'/path/to/folder/'   #else use  this line

 where you put the correct path in the appropriate location

Follow below steps for manual import -
Use below python code to manually import the module file

How to use your function in your code:-
1.  import that module into your python script session with a command like
import myfunc

2.  for using a function "my_sqrt" from your library myfunc
x = myfunc.my_sqrt(val)

3.  you can create alias for your library also
import myfunc as mf
x = mf.my_sqrt(val)

If want to import a particular peice from library, use as
from myfunc import my_sqrt
x = my_sqrt(val)

this is tedious if you have to import multiple so use as
from myfunc import *
x = my_sqrt(val)

But, remember if you are import multiple library and they having same function name.
While using, you need to call them as step 2

Like the below page to get update!forum/datagenx