Pandas cheat sheets

Pandas cheat sheets Find duplicates by column value: df[df.duplicated(['col_name'])] Find duplicates by row: df[df.duplicated()] Select rows from a DataFrame based on column values df.loc[df['column_name'] == some_value] Rename df column df.rename(columns={'gdp':'log(gdp)'}, inplace=True) Datatype of the columns df.dtypes

November 20, 2020 · 1 min

Json to Html table

Convert a JSON Doc or JSON-Schema into a HTML Table <!-- JS-Code taken from https://gist.github.com/fbstj/1199018 --> <!-- See live demo at: https://jsfiddle.net/peLjkfrm/3/ --> <style> table { border-collapse: collapse; } table, th, td { border: 1px solid black; } </style> <div id="mytab"></div> <script> const schema = { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "title": "Book", "properties": { "id": {"type": "string"}, "title": {"type": "string"}, "author": {"type": "string"}, "year": {"type": "integer"}, "publisher": {"type": "string"}, "website": {"type": "string"}, "subDoc": { "type": "object", "properties": { "internal": { "type": "object", "properties": { "ean": { "type": "integer" }, "sn": { "type": "string" } }, "required": [ "ean" ] } }, "required": [ "internal" ] } }, "required": ["id", "title", "author", "year", "publisher", "subDoc"] }; function HTMLON(o, e) { function ne(name) { return $(document.createElement(name)); } function nr(i, v) { return ne('tr').append(ne('th').text(i)).append(HTMLON(v, ne('td'))); } if ($.isArray(o)) { var ul = ne('ul'); $.each(o, function(i, v) { ul.append(HTMLON(v, ne('li'))); }); e.append(ul); } else if (typeof o == 'object') { var t = ne('table'); $.each(o, function(i, v) { t.append(nr(i, v)); }); e.append(t); } else if(typeof o == 'function') { e.append(ne('code').text(o.toString())); } else { e.text(o); } return e; } HTMLON(schema, $('#mytab')); </script>

October 18, 2019 · 1 min

Move files and folders

Move files and folder to another destination using python. import os basedir = 'files' new_dst = "new_dst_folder/" for folder in os.listdir(basedir): inner_path = basedir + folder + "/" for file in os.listdir(inner_path): shutil.move(os.path.join(inner_path, file), new_dst)

June 19, 2019 · 1 min

Rename using Python

The following code snippet renames multiple subfolders and files. import os basedir = "./" for folder in os.listdir(basedir): new_folder_name = folder.translate(str.maketrans({" ": r"-",".": r"_"})) + "_" os.rename(os.path.join(basedir, folder), os.path.join(basedir, new_folder_name) ) for folder in os.listdir(basedir): inner_path = basedir + folder + "/" for file in os.listdir(inner_path): os.rename(os.path.join(inner_path, file), os.path.join(inner_path, folder + file) )

June 2, 2019 · 1 min