Pandas

Python Pandas
Pandas : Python Data analysis tool.

Along with basic understanding of Python, knowledge on these sections are required before learning Pandas.

How to install Python Pandas library
pip install pandas
Getting the version of Pandas
import pandas as pd 
print("Pandas Version : ",pd.__version__)
Upgrade Pandas
pip install --upgrade pandas
Filtering columns and create new DataFrame
my_new = my_data.filter(['equipment','category'],axis=1)
OR
cols=['MATH','ENGLISH']
my_new=my_data[cols]
Displaying columns of the DataFrame
print(my_new.columns)
Number of rows in DataFrame.
print(len(my_new))
Pandas Series Pandas DataFrame Atributes Methods
append() Adding rows list , dictionary to DataFrame
Settings Managing DataFrame display option values
apply Applying function along an Axis or elements
atGet and Set data using rows and columns
astypeCast to a specified dtype
cutUsing segments for categorizing values
describeDescriptive statistics of DataFrame or series
groupbycombining data and aggregate functions
get_dummiesConvert categorical variable into dummy/indicator variables
headFirst n rows of the DataFrame
isnullChecking NaN or None data
ilocValues at different position using integer
infoDetail information about the Dataframe
locValues at different position using column label
filterCondition based filtering of rows
fillnaFilling NaN data
mergecombining DataFrame and aggregate functions
methodsPandas DataFrame methods
nlargestn elements in descending sorted values
maskconditional replacement of data
queryFiltering data by using conditions
countNumber of rows or columns with different options
maxMax value of required axis
meanMean value of required axis
minMin value of required axis
shape, size, ndimDimensions of the DataFrame
stdStandard Deviation on required axis
sumSum of values of required axis
set_indexCreating index using one or more columns
sort_valuesSort columns in ascending or descending
tolistFrom DataFrame to List
notnullNot None and Not Null values checking
pivot()Reshaped data based on column values
pivot_table()Reshaped data with aggregate functions based on column values
reset_indexRemove index of the DataFrame
value_countscounts of unique values
uniqueUnique Data of a column
whereData updating based on condition


Five important basic Pandas DataFrame functions info(), head(), tail(), shape,size,ndim, columns



Creating Pandas DataFrame by using Numpy ndarray
validate_email
DataFrame Style
Pandas Date & Time

Exercises
Exercise1Basic data handling , DataFrame
Exercise1-1Using cut(), groupby and plotting graphs
Exercise-AdvUsing groupby and merge of DataFrame
Exercise2Using str.contains(), max(), min(),len() of DataFrame
Exercise3Using date and time functions with groupby of DataFrame
Exercise3-2Using date and time functions of DataFrame
Exercise3-3Using date and time functions with groupby
Exercise3-4Using date and time with where timedelta64

I/O : Input and output Data from Pandas ( Excel , MySQL , JSon)

We can’t store data in Pandas DataFrame. We can process the data by using Pandas DataFrame after reading data from different sources. Similarly after processing we can save data in different files or database by using available tools.
Data input and output from Pandas DataFrame
Filtering records
locValues at different position using column label
rowsFiltering rows based on data

handling string using str methods

str.containsstring matching against data columns
str.contains.sumMax Min Sum of any column
Convert CaseLower to Upper and vice versa
split()Breaking string using delimiter
slice()Substring by breaking string
cat()Concatenate strings
count()Number of occurrences of pattern
replace()Replace part of string by regex
len()Length of the data in our DataFrame
Plotting Graphs using Data
Plotting graphsCreating different type of graphs using DataFrame
MySQL dataCreating different type of graphs MySQL data
ExerciseGraph using data from Excel file
Managing Date
Pandas Date and time Managing Date and time in Pandas DataFrame

Excel to MySQL

Excel to MySQL
import pandas as pd 
my_data = pd.read_excel('D:\emp.xlsx')
# reading data from root of D drive. 
from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://userid:password@localhost/my_tutorial")

### Creating new table emp or appending existing table 
my_data.to_sql(con=engine,name='emp',if_exists='append')

MySQL to Excel

MySQL to Excel
import pandas as pd 

from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://userid:password@localhost/my_tutorial")

sql="SELECT * FROM emp "
my_data = pd.read_sql(sql,engine )

my_data.to_excel('D:\emp2.xlsx')

Columns and rows

columnsList of column headers of a DataFrame
renamerename columns of DataFrame
add_suffixadding suffix to column names of a DataFrame
add_prefixadding prefix to column names of a DataFrame
dropDelete columns or rows

Data Cleaning

Various methods in Pandas to clean data. Read from different sources then remove or fill with matching data in rows and columns

Get the list of all functions of Pandas. ( Used dir() )
import pandas as pd
print(len(dir(pd))) # 139
for i in dir(pd):
    print(i)
Pandas DataFrame

Parameters of functions

There are some common parameters used in Pandas functions. Understanding them will help in quick learning of functionality.
inplaceBoolean ( True / False ), Result is written back to same dataframe ( True ). The source dataframe is changed. False otherwise.


Upload Excel or CSV file in google drive and connect from Colab Python platform using mount drive


Sample student DataFrame
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer