In [3]:
#comments in Python
'''multiple lines of comments are being shown here'''
Out[3]:
In [1]:
2+3+5
Out[1]:
In [2]:
66-3-(-4)
Out[2]:
In [3]:
32*3
Out[3]:
In [4]:
2**3
Out[4]:
In [5]:
2^3
Out[5]:
In [6]:
43/3
Out[6]:
In [7]:
43//3
Out[7]:
In [8]:
43%3
Out[8]:
In [9]:
import math as mt
In [10]:
mt.exp(2)
Out[10]:
In [11]:
mt.log(10)
Out[11]:
In [12]:
mt.exp(1)
Out[12]:
In [14]:
mt.log(8,2)
Out[14]:
In [15]:
mt.sqrt(1000)
Out[15]:
In [17]:
import numpy as np
In [18]:
np.std([23,45,67,78])
Out[18]:
In [20]:
dir(mt)
Out[20]:
In [21]:
type(1)
Out[21]:
In [22]:
type("Ajay")
Out[22]:
In [24]:
type([23,45,67])
Out[24]:
In [25]:
a=[23,45,67]
In [32]:
len(a)
Out[32]:
In [31]:
np.std(a)
Out[31]:
In [28]:
np.var(a)
Out[28]:
In [30]:
123456789123456789*9999999999999999
Out[30]:
In [35]:
np.random??
In [37]:
from random import randrange,randint
In [39]:
print(randint(0,90))
In [42]:
randrange(1000)
Out[42]:
In [46]:
for x in range(0,10):
print(randrange(10000000000000000))
In [51]:
def mynewfunction(x,y):
taxes=((x-1000000)*0.35+100000-min(y,100000))
print(taxes)
In [53]:
mynewfunction(2200000,300000)
In [54]:
import os as os
In [57]:
os??
In [62]:
for x in range(0,30,6):
print(x)
In [63]:
def mynewfunction(x,y):
z=x**3+3*x*y+20*y
print(z)
In [65]:
for x in range(0,30,6):
mynewfunction(x,10)
In [1]:
import os as os
In [2]:
os.getcwd()
Out[2]:
In [3]:
os.listdir()
Out[3]:
In [4]:
os.chdir('C:\\Users\\Dell')
In [7]:
mystring='Hello World'
In [8]:
mystring
Out[8]:
In [9]:
mystring[1]
Out[9]:
In [10]:
mystring[0]
Out[10]:
In [11]:
print(mystring)
In [12]:
type(mystring)
Out[12]:
In [13]:
len(mystring)
Out[13]:
In [15]:
newstring2='Aye aye me heartie\'s'
In [16]:
newstring3="Aye aye me heartie's"
In [18]:
10*newstring3
Out[18]:
In [30]:
ne1= "'Ajay','Vijay','Anita','Ankit'"
In [32]:
type(ne1)
Out[32]:
In [31]:
str(ne1)
Out[31]:
In [33]:
ne1[1]
Out[33]:
In [26]:
ne2= ['Ajay','Vijay','Anita','Ankit']
In [27]:
str(ne2)
Out[27]:
In [29]:
ne2[1]
Out[29]:
In [34]:
myname1='Ajay'
myname2='John'
In [35]:
message= "Hi I am %s howdy"
In [37]:
message %myname1
Out[37]:
In [36]:
message %myname2
Out[36]:
In [38]:
ne2
Out[38]:
In [40]:
ne2.append('Anna')
In [41]:
ne2
Out[41]:
In [42]:
del ne2[0]
In [43]:
ne2
Out[43]:
In [44]:
ne3=('Sachin','Dhoni','Gavaskar','Kapil')
In [46]:
dir(ne3)
Out[46]:
In [52]:
favourite_movie=['micky mouse,steamboat willie', 'vijay,slumdog millionaire', 'john,passion of christ', 'donald,arthur']
In [53]:
type(favourite_movie)
Out[53]:
In [51]:
favourite_movie2={'micky mouse:steamboat willie', 'vijay:slumdog millionaire', 'john:passion of christ', 'donald:arthur'}
In [54]:
type(favourite_movie2)
Out[54]:
In [55]:
favourite_movie3={'micky mouse':'steamboat willie', 'vijay':'slumdog millionaire', 'john':'passion of christ', 'donald':'arthur'}
In [56]:
type(favourite_movie3)
Out[56]:
In [57]:
favourite_movie3['micky mouse']
Out[57]:
In [58]:
import re
In [59]:
names =["Anna", "Anne", "Annaporna","Shubham","Aruna"]
In [60]:
for name in names:
print(re.search(r'(An)',name))
In [61]:
for name in names:
print(re.search(r'(A)',name))
In [62]:
for name in names:
print(re.search(r'(a)',name))
In [63]:
for name in names:
print(bool(re.search(r'(a)',name)))
In [64]:
import numpy as np
In [74]:
numlist=["$10000","$20,000","30,000",40000,"50000 "]
In [75]:
for i,value in enumerate(numlist):
print(i)
print(value)
In [76]:
for i,value in enumerate(numlist):
numlist[i]=re.sub(r"([$,])","",str(value))
numlist[i]=int(numlist[i])
In [77]:
numlist
Out[77]:
In [68]:
np.mean(numlist)
Out[68]:
In [78]:
from datetime import datetime
In [82]:
datetime.now()
Out[82]:
In [80]:
date_obj=datetime.strptime("15/August/2007","%d/%B/%Y")
In [81]:
date_obj
Out[81]:
In [85]:
a=date_obj-datetime.now()
In [86]:
a.days
Out[86]:
In [88]:
a.seconds
Out[88]:
In [89]:
os.getcwd()
Out[89]:
In [93]:
import IPython
print (IPython.sys_info())
In [94]:
%load_ext version_information
%version_information
Out[94]:
In [90]:
os.chdir('C:\\Users\\Dell\\Downloads')
In [91]:
os.listdir()
Out[91]:
In [99]:
import glob as glob
In [100]:
path = os.getcwd()
extension = 'csv'
os.chdir(path)
result = [i for i in glob.glob('*.{}'.format(extension))]
print(result)
In [95]:
import pandas as pd
In [97]:
fraud=pd.read_csv('ccFraud.csv')
In [104]:
mtcars=pd.read_csv("https://vincentarelbundock.github.io/Rdatasets/csv/datasets/mtcars.csv")
In [101]:
smalldiamonds=pd.read_csv("C:\\Users\\Dell\\Desktop\\Diamond (8).csv")
In [111]:
fraud.columns
Out[111]:
In [110]:
fraud.shape
Out[110]:
In [120]:
len(fraud)
Out[120]:
In [121]:
len(fraud.columns)
Out[121]:
In [109]:
fraud.dtypes
Out[109]:
In [102]:
fraud.info()
In [105]:
mtcars.info()
In [106]:
smalldiamonds.info()
In [108]:
fraud.head()
Out[108]:
In [112]:
fraud.tail()
Out[112]:
In [113]:
fraud2=fraud.copy()
In [115]:
fraud.describe()
Out[115]:
In [116]:
fraud.gender.describe()
Out[116]:
In [117]:
mtcars.head()
Out[117]:
In [118]:
mtcars=mtcars.drop("Unnamed: 0",1)
In [119]:
mtcars.head()
Out[119]:
In [124]:
import IPython
print (IPython.sys_info())
In [125]:
!pip install version_information
%load_ext version_information
%version_information
!pip freeze
In [127]:
!pip install guppy
In [128]:
fraud.head()
Out[128]:
In [135]:
fraud.head().gender
Out[135]:
In [133]:
fraud.gender.head()
Out[133]:
In [132]:
fraud['gender'].head()
Out[132]:
In [131]:
fraud[['gender','state','balance']].head()
Out[131]:
In [136]:
fraud.ix[10:20]
Out[136]:
In [137]:
fraud.iloc[:,:]
Out[137]:
In [139]:
fraud.iloc[10:20,1:4]
Out[139]:
In [140]:
fraud.describe()
Out[140]:
In [141]:
fraud.gender.value_counts()
Out[141]:
In [142]:
fraud.state.value_counts()
Out[142]:
In [143]:
fraud.fraudRisk.value_counts()
Out[143]:
In [144]:
pd.crosstab(fraud.fraudRisk,fraud.gender)
Out[144]:
In [145]:
pd.crosstab(fraud.fraudRisk,fraud.gender,margins=True)
Out[145]:
In [148]:
np.random.choice(100,10)
Out[148]:
In [150]:
a=len(fraud)
In [151]:
b=0.0001
In [152]:
a*b
Out[152]:
In [154]:
fraud.ix[np.random.choice(len(fraud),a*b)]
Out[154]:
In [156]:
! pip install pandasql
In [163]:
from pandasql import sqldf
pysqldf = lambda q: sqldf(q, globals())
In [157]:
mtcars.head()
Out[157]:
In [164]:
pysqldf("SELECT * FROM mtcars LIMIT 10;")
Out[164]:
In [165]:
pysqldf("SELECT * FROM mtcars WHERE gear > 4;")
Out[165]:
In [166]:
pysqldf("SELECT AVG(mpg),gear FROM mtcars group by gear ;")
Out[166]:
In [167]:
mtcars.mpg.mean()
Out[167]:
In [169]:
g1=pd.groupby(mtcars,mtcars.gear)
In [170]:
g1.mean()
Out[170]:
In [171]:
mtcars.gear.value_counts()
Out[171]:
In [173]:
mtcars.cyl.unique()
Out[173]:
In [174]:
pd.crosstab(mtcars.gear,mtcars.cyl)
Out[174]:
In [175]:
mtcars.pivot_table(index='gear', columns='cyl', values='mpg', fill_value=0)
Out[175]:
In [176]:
fraud.head()
Out[176]:
In [181]:
del fraud['custID']
In [182]:
fraud.head()
Out[182]:
In [183]:
fraud3=fraud
In [186]:
del fraud['state']
In [187]:
fraud3.head()
Out[187]:
In [190]:
wine=pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data",header=None)
In [191]:
wine.info()
In [200]:
wine.columns=['WineClass','Alcohol','Malic acid','Ash','Alcalinity of ash','Magnesium','Total phenols','Flavanoids','Nonflavanoid phenols','Proanthocyanins','Color intensity','Hue','OD280/OD315 of diluted wines','Proline']
In [201]:
wine.info()
In [202]:
wine.head()
Out[202]:
In [204]:
wine.WineClass.value_counts()
Out[204]:
In [205]:
classby=pd.groupby(wine,wine.WineClass)
In [206]:
classby.mean()
Out[206]:
In [207]:
wine.describe()
Out[207]:
In [212]:
wine.Ash.describe()
Out[212]:
In [ ]: