Pandas
Plotting
To plot histogram from dataframe
df[anything].hist(bins =50)If you the plot to be separated by categorical variables. for eg final_close_flag has values 0 and 1
df.hist(column="record_number", by="final_close_flag", bins=20)Box plot
df.boxplot(column='name')Segregation by features
df.boxplot(column='name', by='feature')To use plotly as backend to plot directly from pandas dataframe
import pandas as pd
import plotly.graph_objects as go
pd.options.plotting.backend = 'plotly'
df["Close"].plot()To plot candlestick chart using plotly
fig = go.Figure(data=[go.Candlestick(x=df.index, open=df["Open"],
high=df["High"], low=df["Low"], close=df["Close"])])
fig.show()Links
https://www.analyticsvidhya.com/blog/2016/01/12-pandas-techniques-python-data-manipulation/ https://www.analyticsvidhya.com/blog/2016/01/guide-data-exploration/ https://www.analyticsvidhya.com/blog/2015/09/hypothesis-testing-explained/
Tips
List missing values of each column
Boolean Indexing in Pandas
Cut function for binning
Sometimes numerical values make more sense if clustered together. For example, if we’re trying to model traffic (#cars on road) with time of the day (minutes). The exact minute of an hour might not be that relevant for predicting traffic as compared to actual period of the day like “Morning”, “Afternoon”, “Evening”, “Night”, “Late Night”. Modeling traffic this way will be more intuitive and will avoid overfitting.
Here we define a simple function which can be re-used for binning any variable fairly easily.
Coding nominal data using Pandas
Often, we find a case where we’ve to modify the categories of a nominal variable. This can be due to various reasons:
Some algorithms (like Logistic Regression) require all inputs to be numeric. So nominal variables are mostly coded as 0, 1….(n-1) Sometimes a category might be represented in 2 ways. For e.g. temperature might be recorded as “High”, “Medium”, “Low”, “H”, “low”. Here, both “High” and “H” refer to same category. Similarly, in “Low” and “low” there is only a difference of case. But, python would read them as different levels. Some categories might have very low frequencies and its generally a good idea to combine them. Here I’ve defined a generic function which takes in input as a dictionary and codes the values using ‘replace’ function in Pandas.
Show missing values
Check if there are empty rows or columns and find them
Output
3
Andrew
3
5
Laptop
2
Here .any() select a row if one of the columns is 'NA' . Internally a numpy mask df.isnull().any(axis=1) is created which outputs following
List all individual values from categorical columns and their count
Bubble plot
Dataframe
Country O
2008
28.688524590163933
Country O
2004
25.68807339449541
Country O
1996
5.405405405405405
Country O
2002
17.094017094017094
Country O
1992
18.72791519434629
Plotly Version
SeaBorn Version
Last updated