EDA_CODE_SNIPPETS
EDA_CODE_SNIPPETS
Descriptions
1
• Find the covariance matrix:
df . cov ()
• Rename a column:
df . rename ( columns ={ ’ old_name ’: ’ new_name ’} ,
inplace = True )
2
• Reset index of DataFrame:
df . reset_index ( drop = True , inplace = True )
• Drop duplicates:
df . drop_duplicates ( inplace = True )
3
NumPy Snippets (30+ Operations)
• Create an array:
np . array ([1 , 2 , 3])
• Reshape an array:
np . reshape ( arr , ( rows , cols ) )
4
• Transpose an array:
arr . T
• Sort an array:
np . sort ( arr )
• Concatenate arrays:
np . concatenate (( arr1 , arr2 ) , axis =0)
5
np . all ( arr )
6
df [ ’ column_name ’]. mean ()
• Rename a column:
df . rename ( columns ={ ’ old_name ’: ’ new_name ’} ,
inplace = True )
7
df . groupby ( ’ column_name ’) . mean ()
8
df [ df . duplicated ([ ’ column_name ’]) ]
• Drop duplicates:
df . drop_duplicates ( inplace = True )
9
np . zeros ((3 , 3) )
• Reshape an array:
np . reshape ( arr , ( rows , cols ) )
• Transpose an array:
arr . T
10
• Find eigenvalues and eigenvectors:
np . linalg . eig ( arr )
• Sort an array:
np . sort ( arr )
• Flatten an array:
11
arr . flatten ()
• Slice an array:
arr [1:3]
12
• Create a histogram:
plt . hist ( data , bins =10)
• Create a subplot:
plt . subplot (2 , 1 , 1)
plt . plot (x , y )
• Create a heatmap:
plt . imshow ( data , cmap = ’ hot ’)
13
• Add legend to the plot:
plt . legend ([ ’ Label1 ’ , ’ Label2 ’])
• Plot a function:
x = np . linspace (0 , 10 , 100)
plt . plot (x , np . sin ( x ) )
14
• Create a quiver plot:
plt . quiver (x , y , u , v )
• Create a 3D plot:
ax = plt . axes ( projection = ’3d ’)
ax . plot3D (x , y , z )
15
sns . histplot ( df [ ’ column ’] , kde = True )
\ item \ textbf { Add a legend :}
\ begin { lstlisting }
plt . legend ([ ’ Label1 ’ , ’ Label2 ’])
• Save a figure:
plt . savefig ( ’ figure . png ’)
• Create a 3D plot:
from mpl_toolkits . mplot3d import Axes3D
fig = plt . figure ()
ax = fig . add_subplot (111 , projection = ’3d ’)
ax . scatter (x , y , z )
• Create a heatmap:
plt . imshow ( data , cmap = ’ hot ’ , interpolation = ’
nearest ’)
plt . colorbar ()
• Add annotations:
plt . annotate ( ’ Point ’ , xy =( x , y ) , xytext =( x +1 ,
y +1) , arrowprops = dict ( facecolor = ’ black ’ ,
arrowstyle = ’ - > ’) )
16
• Create a violin plot:
plt . violinplot ( data )
17