**Python Pandas Cheat Sheet**

**Simple**, **expressive** and **arguably one of the most important ****libraries** in Python, not only does it make real-world **Data Analysis** significantly easier but provides an optimized feature of being significantly fast.

It is common that for those **who just started Data Science journey** with** Python** and **Pandas library** might find it overwhelming to remember all those functions and their operations, that is when our **cheat-sheet** comes in handy.

**Pandas cheat sheet** will help you through the basics of the Pandas library such as working with DataFrames, Importing and Exporting conventions, Functions, Operations also Plotting DataFrames in different formats

Also, if you want to see an illustrated version of this topic with an example on a real-world dataset you can refer to our **Tutorial Blog on ****Pandas.**

For a better understanding of Python for Data Science through our interactive **Data Science Course** is a must complete, where libraries like Pandas, Numpy and many more are explained in a detailed manner.

Download a Printable PDF of this Cheat Sheet

**Import Convention: **

We need to import the library **before we get started**.

**import pandas**as

**pd**

**Pandas Data Structure:**

We have two types of data structures in **Pandas**, **Series** and **DataFrame**.

**Series** is a one-dimensional labeled array that can hold any data type.

**DataFrame** is a two-dimensional, potentially heterogeneous tabular data structure.

Or we can say **Series** is the data structure for a single column of a **DataFrame**

Now let us see some **examples** of Series and DataFrames for better understanding.

__Series:__**s**= pd.Series([1, 2, 3, 4], index=[‘a’, ‘b’, ‘c’, ‘d’])

__Data Frame:__**data_mobile**= {‘Mobile’: [‘iPhone’, ‘Samsung’, ‘Redmi’], ‘Color’: [‘Red’, ‘White’, ‘Black’], ‘Price’: [High, Medium, Low]}

**df**= pd.DataFrame(

**data_mobile**, columns=[‘Mobile’, ‘Color’, ‘Price’])

**Importing Convention:**

**Pandas library** offers a set of ** reader functions** that can be performed on a wide range of

**file**

**pd.read_csv(**“filename”

**)**

**pd.read_table(“**filename”

**)**

**pd.read_excel(**“filename”

**)**

**pd.read_sql(**query, connection_object

**)**

**pd.read_json(**json_string

**)**

**formats** which returns a **Pandas object. **Here we have mentioned a list of reader functions.

Similarly, we have a list of ** write operations** which are useful while writing data into a file.

**df.to_csv(**“filename”

**)**

**df.to_excel(**“filename”

**)**

**df.to_sql(**table_name, connection_object

**)**

**df.to_json(**“filename”

**)**

**Create Test/Fake Data:**

Pandas library allows us to create fake or test data in order to test our code segments. Check out the examples given below.

**pd.DataFrame(np.random.rand(4,3))**– 3 columns and 4 rows of random floats

**pd.Series(new_series)**– Creates a series from an iterablenew_series

**Operations:**

Here we have mentioned various inbuilt functions and their operations.

**View DataFrame contents:**

**df.head(n) –**look at first n rows of the DataFrame.

**df.tail(n) –**look at last n rows of the DataFrame.

**df.shape() –**Gives the number of rows and columns.

**df.info() –**Information of Index, Datatype and Memory.

**df.describe() –**Summary statistics for numerical columns.

### Watch this Python Interview Questions video

**Selecting:**

we want to select and have a look at a chunk of data from our DataFrame. There are two ways of achieving the same.

First, selecting by position and second, selecting by label.

**Selecting by position using iloc:**

**df.iloc[0] – S**elect first row of data frame

**df.iloc[1] –**Select second row of data frame

**df.iloc[-1] –**Select last row of data frame

**df.iloc[:,0] –**Select first column of data frame

**df.iloc[:,1] –**Select second column of data frame

**Selecting by label using loc:**

**df.loc([0], [column labels])**-Select single value by row position & column labels

**df.loc[‘row1′:’row3’, ‘column1′:’column3’]**-Select and slicing on labels

**Sorting:**

Another very simple yet useful feature offered by Pandas is the sorting of DataFrame.

**df.sort_index()**-Sorts by labels along an axis

**df.sort_values(column1)**– Sorts values by column1 in ascending order

**df.sort_values(column2,ascending=False)**– Sorts values by column2 in

**Groupby:**

Using groupby technique you can create a grouping of categories and then it can be helpful while applying a function to the categories. This simple yet valuable technique is used widely in data science.

**df.groupby(column)**– Returns a groupby object for values from one column

**df.groupby([column1,column2])**– Returns a groupby object values from multiple columns

**df.groupby(column1)[column2].mean()**– Returns the mean of the values in column2, grouped by the values in column1

**df.groupby(column1)[column2].median()**– Returns the mean of the values in column2, grouped by the values in column1

**Functions:**

There are some special methods available in Pandas which makes our calculation easier. Let’s

**Mean**:df.mean() – mean of all columns

**Median**:df.median() – median of each column

**Standard Deviation**:df.std() – standard deviation of each column

**Max**:df.max() – highest value in each column

**Min**:df.min() – lowest value in each column

**Count**:df.count() – number of non-null values in each DataFrame column

**Describe**:df.describe() – Summary statistics for numerical columns

apply those methods in our Product_ReviewDataFrame

**Plotting:**

Data Visualization with Pandas is carried out in the following ways.

- Histogram
- Scatter Plot

Note: Call *%matplotlib inline* to set up plotting inside the Jupyter notebook.

**Histogram:**df.plot.hist()

**Scatter Plot:**df.plot.scatter(x=’column1′,y=’column2′)

**Download a Printable PDF of this Cheat Sheet**

We have covered all the basics of Pandas in this cheat sheet. If you want to start learning NumPy in depth then check out the Python Certification Training by Intellipaat. Not only will you get to learn and implement NumPy with a step by step guidance and support from us, but you will also get to learn some other important libraries in python such as SciPy, NumPy, MatPlotLib,Scikit-learn, Pandas, Lambda function and more. You will also get 24*7 technical support to help you with any and all of your queries, from the experts in the respective technologies here at intellipaat throughout the certification period. Also you will be provided with free Python interview questions asked by the experts during interviews.

For all other topics get back to online Python Tutorial.