-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.txt
More file actions
583 lines (436 loc) · 26 KB
/
Copy pathpython.txt
File metadata and controls
583 lines (436 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
****************
* PYTHON *
****************
o Python is a case sensitive language as it treats upper and lower case characters differently.
o Python files are saved by .py extension.
--> Syntax if print() function:
• print(value,…,sep=’ ’ , end=’\n’ , file=sys.stdout)
• Here if we want to print anything then we use “_” but ‘_’ can also be used
• U can use “_” for strings like hello world and’_’ for characters.
• sep is a string inserted between values and default or a space. we cannot add sep in b/w any values.
end is also a string appended after the last value, default , a new line.
to separate arguments we use sep and end after the last argument.
----------------------------------------------------------------------------------------------------------------------------------------------------------
Data types are built in in python hence it supports dynamic typing.
--> Tokens :
it is the samallest element of python.
these are types of tokens.
Identifiers: Name of any constant, variable, function or module
keywords: reserved words in python
Literals/Constants : fixed values
Operators : symbol or word that performs operations
punctuators/delimiters : (),{},[],;,:
# is not a token nor are commments
--> Variables :
Containers that stores values that u can access or change
a variable has 3 components:
1. Identity : it refers to the address of the variable. the address of an object can be checked using method id(variable)
2. Type : it means data types
number : int , float , complex numbers
string : str
boolean : bool
none : None
type() funct is used to dtermine type of variable
Escape sequence : \a = bell ; \b = backspace ; \f = formfeed ; \n = linefeed ; \r = carriage return ; \t = horizontal tab; \v = vertical tab
3. Value : value given to variable
we can also do multiple assignments in python. eg x,y,z = 3,4,"dev"
--< Mutable objects : list , dictionary,sensitive
--< immutable objects : int , float , complex, bool, string, tuple
--> Operators :
Arithmetic : + , - , / , * , // , % , **
Relational : < , > , <= , >= , != , == , or , and , not
Augmented or shorthand assignment : += , -= , *= , /=, %=, **= , //=
--> User Input and Output :
input() == it is used to get data from User
eval() == this function is used to evaluate the value of string. it takes string as argument and evaluates it as number and returns numeric result.
--> Type Conversion:
Explicit : Done by programmer
Implicit : Done by interpreter
-----------------------------------------------------------------------------------------------------------------------------------------------------------
--> User Defined Functions :
syntax:
def func_name(parameters): ==> function definition
statements
func_name(parameters) ==> Function calling
-----------------------------------------------------------------------------------------------------------------------------------------------------------
--> Descision making Statements :
1. if
2. if-else
3. if-elif
4. nested if - else
--> LOOPS :
1. for loop:
they are definite loops
for <control_variaable> in <items in range>:
statements
2. while loop:
they are called indefinite loops
range() function:
range([start] , stop[, step])
------------------------------------------------------------------------------------------------------------------------------------------------------
--> LISTS :
list is a collection of values or an ordered sequence of values/items.
elements are enclosed in [] brackets.
Lists are Mutable
elements need not be of similar type
it is a sequenced data type.
syntax : <list_name> = [e1,e2,e3,...]
List types:
1. Empty list
2. Long list
3. Nested list
Creating list from Existing sequence:
==> Creating list from a sequence(string) : <new_list_name> = list(sequence/string)
==> an empty list with function list() : eg. list2 = list()
==> Creating a list through user input using list() method : eg. list1 = list(input("Enter the values : "))
Creating a list from an existing list :
list5 = list1[:]
list6 = list[1:4]
list7 = list1
Accessing List Elements :
list[5]
Traversing a list :
1. Using 'in' operator inside for loop: for i in list
2. Using range() function : for i in range()
Aliasing :
it simply means copying one list in other
eg . list = ['a' , 'b','c']
temp = list
here temp is an alias(alternate) name for list.
Both these lists are reffering to the same location .
Modifications made in temp will also affect list.
aliasing should be avoided.
Comparing Lists :
[1,2,3,4]<[4,5,6] ==> true :: 1<4
[1,2,3,4]<[1,2,5,3] ==> true :: 3<5
[1,2,3,4]<[1,2,3,2] ==> false :: 4<2
[1,2,3,4]<[0,1,1,3] ==> false :: 1<0
Operations on Lists :
1. Concatenation : List3 = list1 + list2 ; list1 = list + ['45'/"dev"]
2. repetition : * operator replicates the list and creates new list1
3. Membership Testing : it is a operation to check whether a particular element is member of that sequence or not. eg print('o' in name)
4. Slicing : slicing is done through indexing. list[start:stop:step]
5. Indexing : index starts from 0
Nested Lists :
list = [1,2,3,[4,5,6],7]
To access elements of this nested lists we need list[i][j]
Copying Lists :
1. slice our original list and store it into a new list variable : list2 = list1[:]
2. use the built in func list(): list2 = list(list1)
3. use copy() func : import copy library -> list2 = copy.copy(list1)
Built-In Functions :
1. append : list.append(element)
2. Insert : list.insert(index,ele)
3. Extend : list.extend(list2)
4. Index : list.index(ele)
5. Remove : list.remove(ele)
6. Sort : list.sort()
7. Reverse : list.reverse()
Updating List :
we can update lists using = assignment operator
len() : returns length of list. len(list)
sort() : sorts list in ascending ordered. list.sort()
clear() : removes all items from list. list.clear()
count() : counts how many times a element has occured. list.count()
Deletion Operation :
1. pop() : removes element from specified index. list.pop(index). if no index value provided last value will be deleted
2. remove() : used when we know function to be deleted not index. list.remove(element)
3. del() : it removes the specified element and returns modified list. we can also delete elements from a range.
searching lists : a list can be easily searched for values using index() func.
max() : it returns the max value of the list.
min() : it returns the min value from list
Built-in functions with List
Function Description
reduce() apply a particular function passed in its argument to all of the list elements stores the intermediate result and only returns the final summation value
sum() Sums up the numbers in the list
ord() Returns an integer representing the Unicode code point of the given Unicode character
cmp() This function returns 1 if the first list is “greater” than the second list
max() return maximum element of a given list
min() return minimum element of a given list
all() Returns true if all element is true or if the list is empty
any() return true if any element of the list is true. if the list is empty, return false
len() Returns length of the list or size of the list
enumerate() Returns enumerate object of the list
accumulate() apply a particular function passed in its argument to all of the list elements returns a list containing the intermediate results
filter() tests if each element of a list is true or not
map() returns a list of the results after applying the given function to each item of a given iterable
lambda() This function can have any number of arguments but only one expression, which is evaluated and returned.
--------------------------------------------------------------------------------------------------------------------------------------------------------
--> DICTIONARY :
Python dictionary is an unordered collection of items where each item is a key-value pair.
Each key is separated from its value by a colon (:), the items are separated by commas, and the entire dictionary is enclosed in {}
They are mutable.
Keys are unique while values can be repeated.
The values can be of any type but keys must be of immutable data type such as strings, numbers, or tuples.
Creating a Dictionary(Initializing a dictionary) :
<dic_name> = {'key1':'value1' , 'key2':'value2' , 'key3':'value3' , ....}
A dictionary is extremely useful data storage construct for storing and retriveing all key - value pairs by a unique key.
Methods to create a dictionary :
1. To craete an empty dictionary, put curly braces : d1 = {}
2. You can enclose key-value pairs directly in {}
3. The function dict() is used to create a new dictionary with no items
Accessing Elements In a Dictionary :
Use [] : dict['key'] = value
Traversing A Dictionary :
means accessing each element. Done by for loop. : for key in dict
Appending values in dictionary : dict-name[new_key] = new_value
Updating elements in a dictionary : dict_name[existing_key] = new_value OR dict_name1.update(dict_name2)
Removing an item from dictionary :
1. using del() : del dictname[key]
2. using pop() : dictname.pop(key)
Membership function 'in' and functions 'min' , 'max' and 'sum' apply only to keys.
Common Dictionary functions and methods :
1. len() : len(dict)
2. clear() : dict.clear()
3. get() : dict.get(key, default = None) :: default is the value to be returned if key is not present.
4. items() : dict.items() :: it returns content of dictionary as tuple having pairs.
5. keys() : dict.keys() :: it returns the list of key values.
6. values() : dict.values() :: it returns the list of values
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--> NUMPY :
NumPy is a open source module of Python that provides functions for mathematical computation on arrays and matrices.
Arrays in general refer to a named group of homogeneous elements.
NumPy array is a grid of values with same type, and is indexed by a sequence of non-negative integers
==> ARRAY :
It is acontainer which can hold a fixed number of items and these items should be of same data type.
it consists of Element and Index.
Element is the item stored in array
Index is the location of element in array.
After installation to use numpy , it is required to be imported.
import numpy as np
here np is an alias for numpy
NumPy arrays : 1D array (Vector) and 2D array (Matrix) and Multidimensional array (ndarray)
array Method : To create a 1D NumPy array, we can simply pass python list to a array() method
l1 = [2,3,4]
arr1 = np.array(l1)
List elements are separated by commas while array elements are not
fromstring() : arr1 = np.fromstring('1 2 10 12' , dtype=int, sep=' ')
==> Creation of 2D numpy array :
arr1 = np.array([[10,11,12,13] , [21,22,23,24]]) # multiple lists inside a list
We can also access elements of array like this :
1. 1D array : <array_name>[<index>]
2. 2D array : <array_name>[row][col]
type() : we can check the numpy array using this
shape returns dimension of array :
1. 1D array : a1.shape = (4,)
2. 2D array : A.shape = (2,4)
itemsize returns the length of each elelment of array in bytes : like int,str,float,etc.
NumPy Array | List
--------------------------------------------|-----------------------------------------
=> homogeneous elements | => hetrogeneous Elements
=> does not support addition subtarction | => support addition subtarction
=> less memory consumption | => more memory location
=> faster runtime execution | => slower than arrays
=> contiguous memeory location | => not stored contiguously
=> elementwise operation possible | => elementwise operation not possible
==> NumPy Data types : boolean,int,unsigned int, float, complex, strings, objects, records
==> Methods To Create Numpy Arrays :
1. Using empty() : it is used to create a empty array of specified shape and dtype
numpy.empty(shape,[dtype=<datatype>,],[order = 'C' or 'F'])
dtype is data type
shape is dimension
C means row-wise arrangement
F means column-wise arrangement
2. Using zeroes() : it is different from empty()
the only diff is that it returns a new array with all zeroes
numpy.zeroes(shape , dtype=np.<datatype> , order='C/F')
3. Using ones() : it returns a new array with all ones as elements
numpy.ones(shape , dtype=None , order='C')
to create a 2D arrray with constant value : np.full(shape , value)
4. Using arange() : it is used to create from a range
<array_name> = numpy.arange(start , stop , step , dtype)
5. Using linespace() : to prepare array of any range
<array_name> = numpy.linespace(start,stop,no_of_elements)
6. Using copy() : we can create copy of an existing array.
we can also assign a portion of an array to another array.
arr2 = np.copy(arr1)
7. Using reshape() : creation of 2D array from 1D array
it only changes arrangement not the no. of dimensions
np.reshape(array,(2,3))
==> Array Slicing : it is same as slicing of list b/w the two particular index that we want
data[from:to]
eg. 1D : data[1:3] :: elements from index 1 to 2 are printed
eg. 2D : data[1:2 , 1:2] :: 1st row , 2nd column , elements b/w 1 to 2
==> Joins/Concatenation in Arrays :
np.concatenate([arr1,arr2,....] , axis)
Vertical stack : np.vstack[arr1,arr2] : arr1 will be stacked above arr2
Horizontal stack : np.hstack[arr1,arr2] : arr1 will be stacked beside arr2
axis = 0 : concatenates along row axis
axis = 1 : concatenates alng column axis
==> Transpose of Matrix : To find transpose of matrix use matrix_name.T
To find shape of transposed matrix use matrix_name.T.shape
==> Array subsets :
One or more possible subsets can be created or derived from an existing numpy.ndarray.
The created subsets will be numpy.ndarray objects.
==> Splitting Array :
<new_array> = array_split(<array_to_be_splitted> , <no. of splits> , axis<for 2d>)
==> Arithmetic operations on arrays :
in-built functions : add() ; subtract() ; multiply() ; divide()
==> Statistical OPerations on arrays :
1. max() : to find the maximum element
arr1.max() :: for 1D and 2D array
arr1.max(axis=1) :: for max in row
arr1.max(axis=0) :: for max in col
2. min() : same as max()
3. sum() : arr1.sum() :: will add all the elements of 1D and 2D array
arr1.sum(axis=0) :: for column sum
arr1.sum(axis=1) :: for row sum
4. count_nonzero() : it counts no. of non zero elements in array
5. mean() : to find mean of the elements of array
6. median() : to find median
7. mode : there is no mode built-in func in numpy.
we use "scipy" library
import scipy as Statistical
stats.mode()
8. Standard Deviation : its function is present in numpy std()
9. variance : var() function in numpy
------------------------------------------------------------------------------------------------------------------------------------------------------------
--> PANDAS :
Derived from panel data system.
It is a python library for data analysis.
Data analysis is the process of evaluating big data stes using analytical and statistical tools.
In order to work with pandas , we need to import it.
import pandas as pd
** PANDAS DATA STRUCTURE **
1. SERIES :
It represents 1D array of indexed data.
it has two components : array of actual data
array of indices or data labels
Both components are 1D array of same length
==> Create empty series object by series() with no parameter: <Series_obj> = pandas.Series()
The above syntax will create a series with float64 data type.
==> Create a non-empty Series object: <Series_obj> = pd.Series(data , index=idx)
idx is valid numpy datatype
data can be sequence,dictionary,ndarray,scalar value.
(i) Specify data as Python Sequence : <series_obj> = Series(<any python sequence>)
It will return an object of Sreies type
(ii) Specify data as an ndarray : <series_obj> = Series(<any python ndarray>)
(iii) Specify data as a Python Dictionary : <series_obj> = Series(<any python dictionary>)
(iv) Specify data as a Scalar value : <series_obj> = Series(value to be repeated , index to be given)
==> Additional functionality of Series() :
(i) Specifying NaN values in a series object:
sometimes we don't have full data needed for a project.
In this case u can fill missing data by NaN value.
NaN is defined in NumPy module hence we can write np.NaN or None
(ii) Specify index as well as data with Series():
We can specify index as well as value for index in Series type.
Both value and index are sequences.
<Series_obj> = pandas.Series(data = None, index=None)
here we can skip writing data also.
(iii) Specifying Data type along with index and data:
<series_obj> = pandas.Series(data=None, index=None,dtype=None)
(iv) Using a Mathematical Function/Expression to create data array in Series():
<Series_obj> = pandas.Series(index=None, data=<function|expression>)
While creating index array sequence, we can duplicate the indexes.
==> Series Object attributes: <Series_obj>.<attribute_name>
1. index : index of series
2. index.name : used to assign new name to index
3. values : return series as ndarray
4. dtype : return datatype
5. shape : return tuple of shape
6. nbytes : return number of bytes
7. ndim : return number of dimensions
8. size : return no. of elements
9. itemsize : return size of each item
10. hasnans : returns true if NaN values are there
11. empty : returns true if series is empty
12. name: assign name to a Series object
==> Accessing a series object and its elements :
Accessing individual elements from series obj : <Series_obj>[<valid index>]
Extracting Slices from Series Object : <Series_obj>[<valid range>]
==> operations on Series Object :
1. Modifying elements of series object :
<Series_obj>[<index>] = <new_data>
<Series_obj>[start:stop] = <new_data>
2. Renaming Indexes :
<object>.index = <new_index_array>
size should not change
3. head() and tail() functions :
head() is used to fetch first n rows :: <pandas_obj>.head([n])
tail() returns last n rows :: <pandas_obj>.tail([n])
4. Vector Operations on Series objects :
vector operations means that if u apply a function or expression then it is individually applied to each item
5. Arithmetic on Series Objects :
Operations are performed only on matching indexes
for all other indexees it will produce NaN.
here, NaN represents missing data.
6. Filtering Enteries in Series Objects :
<series_obj> [<Boolean expression on Series object>]
7. Sorting on the basis of values :
<Series_obj>.sort_values([ascending=True/False])
u can also skip ascending argument
8. Sorting On the basis of Indexes :
<Series_obj>.sort_index([ascending = True|False])
==> Reindexing : we can change the order of the indices.:: obj.reindex()
2. DataFrame Data Structure :
It stores data in 2D way.
Characteristics of DataFrame data structure :
1. It has 2 index : row(axis=0) and column(axis=1)
2. indexes can be number,string,letters
3. value mutable
4. size mutable
==> CReating and displaying a dataframe :
import pandas as pd
import numpy as np
<dataFrameObj> = panda.DataFrame(<2D data structure> , \[columns = <column sequence>] , [index=<index sequence>])
empty dataframe : DF1 = pd.DataFrame()
We can create dataframe of dictionary,series,ndarray,list, or other dataFrameObj
(i) Creating a DataFrame Obj from 2D dictionary :
dtf = pd.DataFrame(dict,index=None)
(ii) Creating DataFrame Obj from Dictionaries/Lists :
list = [dict1,dict2,...]
dtf = pd.DataFrame(list)
(iii) Creating a DataFrame from a 2D ndarray :
dtf = pd.DataFrame(ndarr , columns=[<it will give heading to col>] , index = [])
(iv) Creating a dtf from 2D dictionary with values as series objects :
series = pd.Series()
dict = {'' : series}
dtf = pd.DataFrame(dict)
(v) Creating a DataFrame Object from another DtaFrame Object :
dtf2 = pd.DataFrame(dft1)
==> DataFrame Attributes : <DataFrameObj>.<attribute>
1. index : return index
2. columns : return column labels
3. axes : returns the list representing both axes
4. dtypes : return dtype
5. size : return size of dtype of each element
6. shape : returns tuple
7. values : return numpy representation
8. empty : indicates whether dtf is empty or not
9. ndim : return dimensions of the dtf
10. T : transpose of matrix
==> Selecting or Accessing Data :
1. Selecting/Accessing a Column :
<DataFrameobj>[<col_name>] OR <DataFrameObj>.<col_name>
2. SElecting or Accessing multiple Columns :
<DataFrameObj>[[col1,col2,...]]
3. Selecting/Accessing a subset from a Dtf using Row/Col Names :
<DataFrame>.loc[<startrow>:<endrow> , <startcol>:<endcol>]
=> To access a row : <DF>.loc[<row label> , :]
=> To access multiple rows : <DF>.loc[<startrow> : <endrow> , :]
=> To access selective columns : <DF>.loc[: ,<start col> : <end col>]
=> To access range of columns from range of rows : <DF>.loc[<startrow> : <endrow> , <startcol> : <endcol>]
4. Selecting rows and cols from a DF :
<DF>.iloc[<start row index> : <end row index> , <start col index> : <end col index>]
5. SElecting/accessing individual value :
<DF>.<col>[<row name or row numeric index>]
OR
<DF>.at[<row label>,<col label>]
<DF>.iat[<row index> , <col index>]
6. Selecting DF rows/cols based on Boolean Conditions :
it will return the boolean value for any condition.
==> Adding / Modifying rows and columns in DF :
<df_obj>.<column_name> = <new_name> :: this will add a new column
<df_obj>.at[ : , <column_name>] = <Values to be modified>
<df_obj>.loc[ : , <column_name>] = <Values to be modified>
<dfobj> = <dfobj>.assign(<colname>=<value for col>)
similar for row
==> MOdifying a single cell :
<DF>.iat[<row position>,<column position>] = <new value>
<DF>.<columnname>[<row name>] = <new value>
==> Deleting / renaming cols and rows :
del<df obj>[<colname>]
==>Renaming rows or col :
<DF>.rename(index={<names dictionary>} , columns = {<names dictionary>} , inplace = False)
eg. topDF.rename(index={'sec A' : 'A' , 'sec B' : 'B' , ....})
to make changes u need to write inplace= True