Skip to content

Instantly share code, notes, and snippets.

View viniroger's full-sized avatar

Vinicius Roggério da Rocha viniroger

View GitHub Profile
@viniroger
viniroger / concat_columns.py
Created August 7, 2018 21:23
Juntar colunas de arquivos diferentes em um só arquivo
# -*- coding: utf-8 -*-
# Script para juntar colunas de 2 arquivos com mesmo número de linhas em um só
import pandas as pd
import csv
# Abrir arquivos com colunas separadas por ";" e decimais por ","
df1 = pd.read_csv('dados.csv',delimiter=';',decimal=',')
df2 = pd.read_csv('outros_dados.csv',delimiter=';',decimal=',')
# Concatenar colunas
@viniroger
viniroger / tricks.R
Created November 20, 2018 10:41
Truques R
# Iniciar tabela vazia
tabela = data.frame()
# Cortar período
serie_model = subset(dados, data > ini_model)
# Último mês de uma sequência de 14 meses
ultimo_mes_prev = tail(seq(as.Date(fim_model), by = "month", length = 14),n=1)
@viniroger
viniroger / lambda_ex.py
Created February 21, 2019 11:16
Example of lambda function use with conditional statment - Python
# Example of Conditional statements using lambda funtion
dates = ['2019-02-20','2019-02-21','2019-02-22']
td = '2019-02-21'
i = 0
# Conditional statement
if dates[i] < td:
print('obs')
else:
print('prev')
# Conditional statement with lambda
@viniroger
viniroger / bash_execution.py
Created February 28, 2019 08:38
Execute bash script using python
from subprocess import check_output
# List files using pattern and get most recent
bashCommand = ("ls -1t %s/saida/arquivos_clientes/cn*.txt | head -1" %path)
# Define file name as last string, striping by '/'
filename = str(check_output(bashCommand, shell=True).strip(), 'utf-8')
remoteFileName = filename.split('/')[-1]
@viniroger
viniroger / print_all_df.py
Created February 28, 2019 08:39
Pretty-print an entire Pandas Series / DataFrame
def print_all_df(self, df):
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(df)
@viniroger
viniroger / fill_random.py
Created March 21, 2019 09:37
Fill a column from a pandas dataframe with random values
import random
random.seed(42)
df['column_name'] = df['column_name'].apply(lambda v: random.random() * 100)
@viniroger
viniroger / randompass.sh
Created March 26, 2019 21:19
Generate random password
openssl rand -base64 8
@viniroger
viniroger / trycatch.R
Created April 22, 2019 19:56
Try catch read CSV file with error function and warning print
diretorio = '/home/user/model'
uid = 1
filename = sprintf('%s/saida/saida_%s/prevaz_%s.csv', diretorio, 'semanal', uid)
values = tryCatch(read.csv(filename,as.is = TRUE),
error = function(e) NA, warning = function(w) print('Without semanal file'))
@viniroger
viniroger / send_mail.sh
Last active July 10, 2019 18:39
Shell script to send e-mail with attachments and body to mails list
envio_email(){
# Escrever corpo do e-mail
echo "Seguem anexos" >> $arq_email
printf "\n" >> $arq_email
# Arquivo de texto no corpo de e-mail
cat $arq_descr >> $arq_email
# Criar lista de e-mails a partir de arquivo
readarray -t lista_emails < <(cat $DIR/helpers/destinatarios_teste.txt | grep $grupo | awk -F',' '{print $1}')
@viniroger
viniroger / zip_files.sh
Created July 10, 2019 21:10
Select files by pattern and zip into new file
ls *VS00* *VSS.* | zip -@ saida/arquivo.zip