## How to calculate percentile (quantile) for each column in pandas dataframe

Here we calculate 0.9th quantile of each column in our dataframe: q = 0.9 for column in df: qr = df[column].quantile(q) print(f”{q*100}% are lower than {qr}”) Here’s a good example to understand quantiles.

## AttributeError: Can only use .dt accessor with datetimelike values

Make sure you convert column to Date & Time properly before calling dt.strftime(): df[‘NewDateTime’] = pd.to_datetime(df.DateTime).dt.strftime(“%d/%m/%Y %H:%M”)

## Plot candlesticks in python (simple example)

import pandas as pd from datetime import datetime import plotly import plotly.graph_objects as go df = pd.read_csv(‘https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv’) fig = go.Figure(data=[go.Candlestick(x=df[‘Date’], open=df[‘AAPL.Open’], high=df[‘AAPL.High’], low=df[‘AAPL.Low’], close=df[‘AAPL.Close’])]) # fig.show() plotly.offline.plot(fig)

## Ternary operator on pandas dataframe

Unfortunately you can use ternary operator like this a if x>y else b on pandas dataframe logic. With that said you can use numpy.where instead: df[‘result’] = np.where(df1[‘col1’] > df1[‘col2′], 1, 0) There you go. It’s also much faster.

## Specify new column names when concatenating pandas dataframes

How to concatenate dataframes and give column new names? Here, look: df2 = pd.concat([df1,df2], keys=[‘x’, ‘y’, ‘z’] ,axis=1)

## Describe built-in function in Python

.describe() in Python And here’s a good image to understand percentiles: Images taken from here and here.

## Pandas set options to display all columns and rows

import pandas as pd pd.set_option(‘display.max_rows’, 500) pd.set_option(‘display.max_columns’, 500) pd.set_option(‘display.width’, 1000)

## How to get column index number by column name in pandas

print(df.columns.get_loc(“column_name”))

## How to reformat time in python with strftime

You need to use ‘dt.strftime‘ df[“new_time”] = df[“time”].dt.strftime( “%d/%m/%Y %H:%M” )