DEV Community

Cover image for Pandas cheat sheet 1
ssenyonga yasin
ssenyonga yasin

Posted on

Pandas cheat sheet 1

Pandas Cheat Sheet for Data Science in Python

A quick guide to the basics of the Python data analysis library Pandas, including code samples.
The Pandas library is one of the most preferred tools for data scientists to do data manipulation and analysis, next to matplotlib for data visualization and NumPy, the fundamental library for scientific computing in Python on which Pandas was built.

The fast, flexible, and expressive Pandas data structures are designed to make real-world data analysis significantly easier, but this might not be immediately the case for those who are just getting started with it. Exactly because there is so much functionality built into this package that the options are overwhelming.

That's where this Pandas cheat sheet might come in handy.

It's a quick guide through the basics of Pandas that you will need to get started on wrangling your data with Python.

As such, you can use it as a handy reference if you are just beginning their data science journey with Pandas or, for those of you who already haven't started yet, you can just use it as a s a guide to make it easier to learn about and use it.

The Pandas cheat sheet will guide you through the basics of the Pandas library, going from the data structures to I/O, selection, dropping indices or columns, sorting and ranking, retrieving basic information of the data structures you're working with to applying functions and data alignment.

Python For Data Science Cheat Sheet:

Pandas Basics
Use the following import convention:

import pandas as pd

Pandas Data Structures

Series

A one-dimensional labeled array capable of holding any data type

s = pd.Series([3, -5, 7, 4], index=['a', 'b', 'c', 'd'])

A   3
B   5
C   7
D   4
Enter fullscreen mode Exit fullscreen mode

DataFrame

A two-dimensional labeled data structure with columns of potentially different types

data = {'Country': ['Belgium', 'India', 'Brazil'],
'Capital': ['Brussels', 'New Delhi', 'Brasilia'],'Population': [11190846, 1303171035, 207847528]}

df = pd.DataFrame(data,columns=['Country', 'Capital', 'Population'])

Country Capital Population
1   Belgium Brussels    11190846
2   India   New Delhi   1303171035
3   Brazil  Brasilia    207847528
Enter fullscreen mode Exit fullscreen mode

Please note that the first column 1,2,3 is the index and Country,Capital,Population are the Columns.

Asking For Help

 help(pd.Series.loc)
I/O
Enter fullscreen mode Exit fullscreen mode

Read and Write to CSV

 pd.read_csv('file.csv', header=None, nrows=5)
 df.to_csv('myDataFrame.csv')
Enter fullscreen mode Exit fullscreen mode

Read multiple sheets from the same file

 xlsx = pd.ExcelFile('file.xls')
df = pd.read_excel(xlsx,  'Sheet1')
Enter fullscreen mode Exit fullscreen mode

Read and Write to Excel

 pd.read_excel('file.xlsx')
 df.to_excel('dir/myDataFrame.xlsx',  sheet_name='Sheet1')
Enter fullscreen mode Exit fullscreen mode

Read and Write to SQL Query or Database Table.

(read_sql()is a convenience wrapper around read_sql_table() and read_sql_query())

from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:')
pd.read_sql(SELECT * FROM my_table;, engine)
pd.read_sql_table('my_table', engine)
pd.read_sql_query(SELECT * FROM my_table;', engine)
df.to_sql('myDf', engine)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)