Pandas
Plotting
To plot histogram from dataframe
If you the plot to be separated by categorical variables. for eg final_close_flag
has values 0 and 1
Box plot
Segregation by features
To use plotly as backend to plot directly from pandas dataframe
To plot candlestick chart using plotly
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
index | Accessories | customer | quantity |
---|---|---|---|
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
Last updated