Getting Started With Python Cheat Sheet
Getting Started With Python Cheat Sheet
A list is an ordered and changeable sequence of elements. It can hold integers, characters, floats, strings, and even objects.
# Create a string with double or single quotes
"""
Learn Python online at www.DataCamp.com List functions and methods A Frame of Data
> How to use this cheat sheet reversed(x) # Reverse the order of elements in x e.g., [2,3,1]
"""
Python is the most popular programming language in data science. It is easy to learn and comes with a wide array of str[0:2] # Get a substring from starting to ending index (exclusive)
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 Selecting list elements
the following cheat sheets below:
Combining and splitting strings
Python lists are zero-indexed (the first element has index 0). For ranges, the first element is included but the last is not.
Mutate strings
Importing data in python Data wrangling in pandas
Concatenating lists
str = "Jack and Jill" # Define str
x = [1, 3, 6]
3 * x # Returns [1, 3, 6, 1, 3, 6, 1, 3, 6] str.lower() # Convert a string to lowercase, returns 'jack and jill'
type('a') # Get the type of an object — this returns str > Getting started with dictionaries
A dictionary stores data values in key-value pairs. That is, unlike lists which are indexed by position, dictionaries are indexed
> Getting started with DataFrames
> Importing packages by their keys, the names of which must be unique.
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
Python packages are a collection of useful tools developed by the open-source community. They extend the
Creating dictionaries columns. A pandas series is a structure that contains one-dimensional data.
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.
# Create
{'a': 1,
a dictionary with {}
'b': 4, 'c': 9}
Creating DataFrames
import pandas # Import a package without an alias
dictionary
# Create a dataframe from a list
pd.DataFrame([
of dictionaries
> The working directory x.values() # Get the values of a dictionary, returns dict_values([1, 2, 3])
}) ])
df['col']
> Operators 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
df[['col1', 'col2']]
df.iloc[:, 2]
df.iloc[3, 2]
# Return a stepped sequence from start (inclusive) to end (exclusive)
pd.concat([df, df])
df.mean()
a = 5 # Assign a value to a
np.repeat([1, 3, 6], 3) # Returns array([1, 1, 1, 3, 3, 3, 6, 6, 6])
# Get rows matching a condition
# Get unique rows
# Rename columns
df.sort_values(by='col_name')
df.nlargest(n, 'col_name')
(1 != 1) & (1 < 1) # Logical AND with & (1 != 1) ^ (1 < 1) # Logical XOR with ^ np.mean(x) # Calculate mean