Pandas / Pyplot / Seaborn

Examples on how to use Pandas, Pyplot, and Seaborn

Pandas    

Iterate


# Create a simple Pandas DataFrame
df = pd.DataFrame([[1,'A',1.23,10],[2,'B',2.34,9],[3,np.nan,float('NaN'),None],[4,'D',4.45,7]], columns = ['idx','cat','float','int'])

print(df.head(), '\n')

# .apply() is very slow
#df['float'] = df['float'].apply(lambda x: x*2)

# Change values using a lambda function
#df['float'] = df['float'].apply(lambda x: x * 2)  # Multiply all values by 2

print(df.head(), '\n')

 


# import necessary libraries
import pandas as pd
import numpy as np

# Create a simple Pandas DataFrame
df = pd.DataFrame([[1,'A',1.23,10],[2,'B',2.34,9],[3,np.nan,float('NaN'),None],[4,'D',4.45,7]], columns = ['idx','cat','float','int'])

print(df.head(), '\n')

# Change specific row values in a column
df.loc[2, 'int'] = 20  # Change the value at row 2 to 20
df.at[3, 'int'] = 25   # Change the value at row 3 to 25

print(df.head(), '\n')

 

Chain


Pivot


# import necessary libraries
import pandas as pd
import numpy as np

# Create a simple Pandas DataFrame
df = pd.DataFrame([[1,'A',1.23,10],[2,'B',2.34,9],[3,np.nan,float('NaN'),None],[4,'D',4.45,7]], columns = ['idx','cat','float','int'])

print(df.head(), '\n')

# Create a pivot table
pivot_table = df.pivot_table(values='float', index='idx', columns='cat')

print(pivot_table.head(), '\n')

Append


Delete


Miscellaneous


Group


Change


Time Series


Convert


import pandas as pd
import numpy as np
df = pd.DataFrame([[1,'A',1.23,10],[2,'B',2.34,9],[3,np.nan,float('NaN'),None],[4,'D',4.45,7]], columns = ['idx','cat','float','int'])

print(df.head(), '\n')
# display count of NAN in column 'float'
print("df['float'].isnull().sum(): ", df['float'].isnull().sum(), '\n')

# replace column with NaN with the value 0
df['float'] = df['float'].fillna(0)

# Replace NaN values for cloumn 'float' with the mean, median, or mode
#print("df['float'].mean() = ", df['float'].mean())
#print("df['float'].median() = ", df['float'].median())
#print("df['float'].mode() = ", df['float'].mode())
#df['float'].fillna(df['float'].median(), inplace=True)

# convert to data type integer
df['float'] = df['float'].astype(int)

# Show the data types and values for the dataframe
print(df.info(),'\n')
print(df.head(),'\n')

 


 

Categorical


Plot


Arithmetic


I/O


Create


DataFrame Info


df.shape
len(df)
df.head(n)
df.tail(n)
df.describe()
df.info()	# stats
df.mean(), df.median(), df.mode(), df.std()
df.dtypes	# data types
df.col1.unique()
df.col1.nunique()
df.index

Rename

Index


df.rename({"idx1": "newidx1"})
df.rename(lambda x:x+1)

Columns


df.rename({"col1":"newcol1"}, axis=1)
df.rename(lambda: x:x+"_col" axis=1)
df.add_suffix('_col')
df.add_prefix('col_')

Sort


Dataframe Subset



Pyplot    



Seaborn