
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

Python is the most popular programming language in data science. It is easy to learn and comes with a wide array of powerful libraries for data analysis. This cheat sheet provides beginners and intermediate users a guide to starting using python. Use it to jump-start your journey with python. If you want more detailed Python cheat sheets, check out the following cheat sheets below:
'a'
help(max) type( )
Python packages are a collection of useful tools developed by the open-source community. They extend the capabilities of the python language. To install a new package (for example, pandas), you can go to your command prompt and type in pip install pandas. Once a package is installed, you can import it as follows.
pandas pandas pd pandas DataFrame
import import as from import
The working directory is the default file path that python reads or saves files into. An example of the working directory is โC://file/path". The os library is needed to set and get the working directory.
os os.getcwd() os.setcwd( )
import # Import the operating system package
"new/working/directory" # Set the working directory to a new file path
division with %
a = x[ ] =
A list is an ordered and changeable sequence of elements. It can hold integers, characters, floats, strings, and even objects.
x = [ 1 , 3 , 2 ]
x. (x) x.sort() (x) x. () x.count( )
sorted
reversed reversed
2 # Count the number of element 2 in the list
Python lists are zero-indexed (the first element has index 0 ). For ranges, the first element is included but the last is not.
x = [ , , , , ] x[ ] x[ ]
'a' 'b' 'c' 'd' 'e' 0
x[ : ] x[ :] x[: ]
x = [ , , ] y = [ , , ]
x + y
3 # Returns [ 1 , 3 , 6 , 1 , 3 , 6 , 1 , 3 , 6]
A dictionary stores data values in key-value pairs. That is, unlike lists which are indexed by position, dictionaries are indexed by their keys, the names of which must be unique.
{ 'a': 1 , 'b': 4 , 'c': 9 }
x = { : , : , : } x.keys() x.values()
'a' 1 'b' 2 'c' 3 # Define the x ditionary
x[ 'a'] # 1 # Get a value from a dictionary by specifying the key
NumPy is a python package for scientific computing. It provides multidimensional array objects and efficient operations on them. To import NumPy, you can run this Python code import numpy as np
1 2 3 # Returns array([ 1 , 2 , 3])
np.array([ , , ])
np.arange( , )
np.arange( , , )
np.repeat([ , , ], )
np.tile([ , , ], )
np.quantile(x, q) np. (x, n) np.var(x) np.std(x)
round
All functions take an array as the input. np.log(x) np.exp(x) np. (x) np. (x) np. (x) np.mean(x)
max min sum
str # Get the character at a specific position
str # Get a substring from starting to ending index (exclusive)
"DataCamp"
"He said, "DataCamp""
A Frame of Data
Tidy, Mine, Analyze It
Now You Have Meaning
Citation: https://mdsr-book.github.io/haikus.html
"""
0 0 2
"Data" "Framed" 3 "data " "beekeepers" "e"
.split( )
pd.concat([df, df])
pd.concat([df,df],axis= )
df.query( )
df.drop(columns=[ ])
df.rename(columns={ : })
df.assign(temp_f= / * df[ ] + )
"columns"
'logical_condition'
'col_name'
"oldname" "newname"
9 5 'temp_c' 32
df.mean()
df.agg(aggregation_function)
df.drop_duplicates()
df.sort_values(by= )
df.nlargest(n, )
'col_name'
'col_name'
Pandas is a fast and powerful package for data analysis and manipulation in python. To import the package, you can use import pandas as pd. A pandas DataFrame is a structure that contains two-dimensional data stored as rows and columns. A pandas series is a structure that contains one-dimensional data.
pd.DataFrame({
: [ , , ],
: np.array([ , , ]),
: [ , , ]
})
'a' 1 2 3 'b' 4 4 6 'c' 'x' 'x' 'y'
pd.DataFrame([
{ : , : , : }, { : , : , : }, { : , : , : }
])
'a' 1 'b' 4 'c' 'x' 'a' 1 'b' 4 'c' 'x' 'a' 3 'b' 6 'c' 'y'
Select a row, column or element from a dataframe. Remember: all positions are counted from zero, not one.
df.iloc[ ]
df[ ]
df[[ , ]]
df.iloc[:, ]
df.iloc[ , ]
'col'
'col1' 'col 2 '
2
3 2
str str str str str
.upper() .lower() .title() .replace( , )
"Jack and Jill"