dataframe format column code example
Example 1: python how to rename columns in pandas dataframe
pandas_dataframe.columns = ['list', 'of', 'column', 'names']
pandas_dataframe.rename(columns={'column_name_to_change':'new_name'})
pandas_dataframe.index = ['list', 'of', 'row', 'names']
pandas_dataframe.rename(index={'row_name_to_change':'new_name'})
Example 2: formatting columns a dataframe python
import pandas as pd
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
index=['foo','bar','baz','quux'],
columns=['cost'])
df['foo'] = df['cost']
df['cost'] = df['cost'].map('${:,.2f}'.format)
print(df)
Example 3: pandas sub columns
| Start | Intermediary | End |
| | | | | | |
_________________________________________________________________
| s_lat | s_lng | i_lat | i_lng | e_lat | e_lng |
mux = pd.MultiIndex.from_product([['Start','Intermediary','End'], ['lat','lng']])
df = pd.DataFrame(data, columns=mux)
Example 4: pandas dataframe caption
styles = [dict(selector="caption",
props=[("text-align", "center"),
("font-size", "120%"),
("color", 'black')])]
output += df.style.set_table_attributes("style='display:inline; font-size:110%' ")
.set_caption(caption)
.set_table_styles(styles)
._repr_html_()