Skip to content

Instantly share code, notes, and snippets.

View tchiavegatti's full-sized avatar

Tiago Chiavegatti tchiavegatti

View GitHub Profile
@tchiavegatti
tchiavegatti / instructions.md
Created April 1, 2018 20:56 — forked from zentralwerkstatt/instructions.md
Install Syncthing on Raspberry Pi
  • Install the necessary packages:
sudo apt-get install apt-transport-https ca-certificates
curl -s https://syncthing.net/release-key.txt | sudo apt-key add -
echo "deb http://apt.syncthing.net/ syncthing release" | sudo tee /etc/apt/sources.list.d/syncthing.list
sudo apt-get update
sudo apt-get install syncthing
sudo apt-get install git
  • Start syncthing once:
@tchiavegatti
tchiavegatti / python-batchfile.bat
Created May 6, 2019 14:08 — forked from jadient/python-batchfile.bat
Run python code directly from a batch file
@echo off & python -x "%~f0" %* & goto :eof
# ==========================================================
# one way to place python script in a batch file
# place python code below (no need for .py file)
# ==========================================================
import sys
print "Hello World!"
for i,a in enumerate(sys.argv):
@tchiavegatti
tchiavegatti / change_column_order
Last active March 10, 2022 21:45
Reorder one or several columns of a dataframe #pandas
def change_column_order(df, col_position):
'''Reorder one or several columns of a dataframe.
Based on the function described at https://stackoverflow.com/a/37071454.
Parameters
----------
df : pandas dataframe
col_position : dict
{'Column' : index where the column should be}
@tchiavegatti
tchiavegatti / check_version.py
Last active November 15, 2021 20:57
[Check python version on a Jupyter notebook] #jupyter
# Check version runing on Jupyter notebook
from platform import python_version
print(python_version())
# Check version inside your Python program
import sys
print(sys.version)
# Check version in command line or shell
python --version
@tchiavegatti
tchiavegatti / working_with_columns.py
Last active November 15, 2021 20:57
[Usefull stuff related to columns on Pandas] #pandas
# Find column index
df.columns.get_loc('col_name')
# The other way around (find column name using the index)
df.columns.get_values()[index]
# Find indices of multiple columns
def column_index(df, query_cols):
``` Get indices of multiple columns.
https://stackoverflow.com/a/38489403
@tchiavegatti
tchiavegatti / func_filepath.py
Created January 31, 2020 19:25
[Storage filepath] Function to set a filepath to a dataframe storage in feather, HDF5, CSV or Excel #pandas
def filepath(filetype, destination, version=None, tag=tag):
"""
Returns a filepath to for file export.
Attributes:
filetype: `str`, 'feather', 'hdf', 'csv', 'excel'
Type of the output file. Implemented to date are feather, hdf5 (h5), csv and Excel (xlsx).
destination: `str`, 'data' or 'output'.
Whether the file should be exported to the data folder or to the output folder. \
HDF5 and feather files storing temporary data can go to the data folder. \
@tchiavegatti
tchiavegatti / combine_xlsx.py
Created February 4, 2020 17:55
[Combine Excel files] #pandas
#!/usr/bin/env python3
from pathlib import Path
import pandas as pd
import xlsxwriter
# Set filename tag
tag = 'client'
# Set filepaths
@tchiavegatti
tchiavegatti / fix_column_names.py
Created February 11, 2020 21:08
[Fix column names] Remove whitespace so you can use the df.ColumnName notation #pandas
# From https://medium.com/@chaimgluck1/working-with-pandas-fixing-messy-column-names-42a54a6659cd
#Sometimes you load in that DataFrame from a csv or excel file that some unlucky excel user created and you just wish everyone used Python. Why do they have to make the column names uppercase, with spaces, and whitespace all around? Do they like doing this to you? They probably hate you, that’s it. They did this:
#The nerve!
#Now you can’t reference the columns with the convenient .{column name here} notation. You’ll have to do the [''] thing. It’s not the most horrible thing that ever happened, you’ve been through a #lot. You can take it. But you’re used to the other way. Plus, what’s with those parentheses? You can’t have that.
#Luckily, pandas has a convenient .str method that you can use on text data. Since the column names are an ‘index’ type, you can use .str on them too. You can fix all these lapses of judgement by #chaining together a bunch of these .str functions. Like so:
df.columns = df.columns.str.s
@tchiavegatti
tchiavegatti / counter.py
Created September 25, 2020 17:08
[Counter] Count values in a list
from collections import Counter
lst = [4, 3, 3, 2, 4, 3]
print(Counter(lst))
Counter({3: 3, 4: 2, 2: 1})
@tchiavegatti
tchiavegatti / ExampleJoinByFussyLookup.py
Last active September 25, 2020 17:41
Join by string match #pandas
#https://towardsdatascience.com/joining-dataframes-by-substring-match-with-python-pandas-8fcde5b03933
import pandas as pd
df1 = pd.DataFrame([
['ABC', 'P1']
, ['BCD', 'P2']
, ['CDE', 'P3']
]
,columns = ['task_name', 'pipeline_name']
)