Skip to content

Instantly share code, notes, and snippets.

@viveksck
viveksck / pd_latex_table.py
Created February 9, 2018 22:14 — forked from tbrittoborges/pd_latex_table.py
Pandas recipe for better latex tables
def better_table(table, caption, name):
start = r"""
\begin{{table}}[!htb]
\sisetup{{round-mode=places, round-precision=2}}
\caption{{{}}}\label{{table:{}}}
\centering
""".format(caption, name)
end = r"\end{table}"
@viveksck
viveksck / gist:64bfb3fbd7937bd11778705f94140e34
Created March 17, 2019 01:22 — forked from seanjtaylor/gist:568141f04a16d518be24
Reshaping a Pandas dataframe into a sparse matrix
import pandas as pd
import scipy.sparse as sps
df = pd.DataFrame({'tag1': ['sean', 'udi', 'bogdan'], 'tag2': ['sean', 'udi', 'udi'], 'freq': [1,2,3]})
# tag1 -> rows, tag2 -> columns
df.set_index(['tag1', 'tag2'], inplace=True)
mat = sps.coo_matrix((df.freq, (df.index.labels[0], df.index.labels[1])))
print(mat.todense())