Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
218 views

Basics Cheat Sheet Python For Data Science: Strings Lists

This document provides an overview of key Python concepts for data science including strings, lists, variables, data types, and NumPy arrays. It covers common operations for each such as indexing, slicing, methods, and basic calculations. Lists can be concatenated, multiplied, and have elements added or removed using methods like append and remove. NumPy arrays are similar to lists but optimized for numerical data.

Uploaded by

Char Siew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
218 views

Basics Cheat Sheet Python For Data Science: Strings Lists

This document provides an overview of key Python concepts for data science including strings, lists, variables, data types, and NumPy arrays. It covers common operations for each such as indexing, slicing, methods, and basic calculations. Lists can be concatenated, multiplied, and have elements added or removed using methods like append and remove. NumPy arrays are similar to lists but optimized for numerical data.

Uploaded by

Char Siew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

> Strings > Lists Also see NumPy Arrays

Python For Data Science >>> my_string = 'thisStringIsAwesome'

>>> my_string

>>>
>>>
a = 'is'

b = 'nice'

Basics Cheat Sheet


'thisStringIsAwesome' >>> my_list = ['my', 'list', a, b]

>>> my_list2 = [[4,5,6,7], [3,4,5,6]]

String Operations
Selecting List Elements Index starts at 0
Learn Python Basics online at www.DataCamp.com >>> my_string * 2

'thisStringIsAwesomethisStringIsAwesome'
Subset
>>> my_string + 'Innit'

'thisStringIsAwesomeInnit'
>>> my_list[1] #Select item at index 1

>>> 'm' in my_string


>>> my_list[-3] #Select 3rd last item
True Slice

> Variables and Data Types String Indexing Index starts at 0


>>>
>>>
>>>
my_list[1:3] #Select items at index 1 and 2

my_list[1:] #Select items after index 0

my_list[:3] #Select items before index 3

>>> my_list[:] #Copy my_list


Variable Assignment >>> my_string[3]

>>> my_string[4:9] Subset Lists of Lists


>>> my_list2[1][0] #my_list[list][itemOfList]

>>> x=5

>>> my_list2[1][:2]
>>> x

5
String Methods
>>> my_string.upper() #String to uppercase
List Operations
Calculations With Variables >>>
>>>
my_string.lower() #String to lowercase

my_string.count('w') #Count String elements


>>> my_list + my_list

>>> my_string.replace('e', 'i') #Replace String elements


['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']

>>> x+2 #Sum of two variables


>>> my_string.strip() #Strip whitespaces >>> my_list * 2

['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']

>>> x-2 #Subtraction of two variables

>>> my_list2 > 4

True
>>> x*2 #Multiplication of two variables

10

>>> x**2 #Exponentiation of a variable

25

> NumPy Arrays Also see Lists


List Methods
>>> x%2 #Remainder of a variable

>>> my_list = [1, 2, 3, 4]

1
>>> my_list.index(a) #Get the index of an item

>>> my_array = np.array(my_list)

>>> x/float(2) #Division of a variable


>>> my_list.count(a) #Count an item

>>> my_2darray = np.array([[1,2,3],[4,5,6]])


2.5 >>> my_list.append('!') #Append an item at a time

>>> my_list.remove('!') #Remove an item

Types and Type Conversion Selecting Numpy Array Elements Index starts at 0 >>>
>>>
del(my_list[0:1]) #Remove an item

my_list.reverse() #Reverse the list

>>> my_list.extend('!') #Append an item

Subset >>> my_list.pop(-1) #Remove an item

str()
>>> my_list.insert(0,'!') #Insert an item

'5', '3.45', 'True' #Variables to strings >>> my_array[1] #Select item at index 1
>>> my_list.sort() #Sort the list
2
int()
Slice
5, 3, 1 #Variables to integers
>>> my_array[0:2] #Select items at index 0 and 1

float()
5.0, 1.0 #Variables to floats
array([1, 2])

Subset 2D Numpy arrays


> Python IDEs (Integrated Development Environment)
bool() >>> my_2darray[:,0] #my_2darray[rows, columns]

array([1, 4])
True, True, True #Variables to booleans
Leading open data science
Free IDE that is included
Create and share

Numpy Array Operations platform powered by Python with Anaconda documents with live code

> Libraries >>> my_array > 3

> Asking For Help


array([False, False, False, True], dtype=bool)

>>> my_array * 2

array([2, 4, 6, 8])

>>> my_array + np.array([5, 6, 7, 8])

Data analysis Scientific computing 2D plotting Machine learning array([6, 8, 10, 12]) >>> help(str)

Import Libraries Numpy Array Functions


>>> import numpy
>>> my_array.shape #Get the dimensions of the array

>>> import numpy as np >>> np.append(other_array) #Append items to an array

>>> np.insert(my_array, 1, 5) #Insert items in an array

>>> np.delete(my_array,[1]) #Delete items in an array

Selective import >>>


>>>
np.mean(my_array) #Mean of the array

np.median(my_array) #Median of the array

>>> my_array.corrcoef() #Correlation coefficient

>>> from math import pi >>> np.std(my_array) #Standard deviation

Learn Data Skills Online at


www.DataCamp.com

You might also like