Posts by lmonferrari • 3,550 points
179 posts
-
1
votes1
answer122
viewsA: Add a new column in a Dataframe after comparing data with another Dataframe?
What you want to do (at least it is what it looks like) does not need to join two data frames, because the data frame quotation itself already brings the information you want. df_cotacao['Moeda'] =…
-
2
votes2
answers35
viewsA: How to apply a ribbon to a dataframe based on the last characters of each label?
A solution similar to Lucas' would be to use the apply lambda df[df['A'].apply(lambda x: x[-3:] == 'BRL')] You could also use split df[df['A'].str.split('-').apply(lambda x: x[1] == 'BRL')] In both…
-
1
votes1
answer82
viewsA: How to group data from another grouping?
A possible solution df['Variação_Salario'] = df.sort_values('Nome').groupby(['Nome'])['Salario'].pct_change().fillna(0).add(1) df['Porcentagem acumulada'] =…
-
1
votes2
answers33
viewsA: Check whether a variable created through exec() exists
You can use dir() Python and iterate over it to check if it exists for e in dir(): if e == 'host5': print('ok') Or that way to check a range of names for i in range(len(dir())): if f'host{i}' in…
-
1
votes4
answers241
viewsA: Filter list of python objects
You can use the lambda expression and test if the ID is 1 lista = [{'ID': 1, 'Name': 'Teste 1' }, {'ID': 2, 'Name': 'Teste 2' }] filtrado = filter(lambda x: x['ID'] == 1, lista) Exit [f for f in…
-
2
votes2
answers63
viewsA: Is there any way pd. Grouper, how much used for time frequencies, adds lines even when there are no records in a time interval?
You can use the function asfreq of pandas import pandas as pd df = pd.read_csv('./Antes do Agrupamento.csv', parse_dates=['Data']) df_agregado = df.groupby(['Numero Agrupado', pd.Grouper(key='Data',…
-
2
votes2
answers97
viewsA: How to filter rows where columns meet consecutive conditions in Python?
You can use isin by creating a list of possible combinations. vl = ['LA','IA','LS','IS'] dados['promo'] = (dados.shift(axis = 1) + dados).isin(vl).any(axis = 1).astype(int) dados.shift 'move' the…
-
3
votes3
answers84
viewsA: Access values from a python json
If you want to work with json instead of using data.text utilize data.json import requests data = requests.get('https://proxycheck.io/v2/42.131.121.100?vpn=1&asn=1') j = data.json()…
-
0
votes2
answers331
viewsA: Problem of wget in python
One way to solve is by using requests import requests import time import re header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'} for i in…
-
1
votes2
answers64
viewsA: Export Summary() as data-frame
An alternative dados1 <- read.csv("dados-originais.csv", header = T, skip = 0, sep = ",") sum(is.na(dados1)) dados1_summary <- data.frame(sapply(na.omit(dados1), summary)) Loading the data…
ranswered lmonferrari 3,550 -
0
votes1
answer18
viewsA: How to separate in x and y being all but the last x and the last y?
You can pass the column names as a list and slice them: def split(dados): SEED = 42367 X = dados[dados.columns.to_list()[:-1]] y = dados[dados.columns.to_list()[-1:]] train_x, test_x, train_y,…
-
0
votes2
answers49
viewsA: How do you sum all the values of the vector in Python?
from functools import reduce valores = [] for i in range(1, 11): valores.append(int(input(f'Informe o {i}º valor: '))) An alternative way is to use the function reduce. It aggregates the values in a…
pythonanswered lmonferrari 3,550 -
3
votes1
answer35
viewsA: How to check the periodicity of a series in the R?
You can use the package Tsstudio with the function ts_info ts_info(sua_serie) Output example: The a series is a xts object with 1 variable and 15 observations Frequency: quarterly Start time:…
-
0
votes2
answers93
viewsA: Date change of a monthly average of Pandas
If the question is only to change the day without changing the average, you can use apply with the replace method: df['data'] = df['data'].apply(lambda d: d.replace(day = 15)) Entree: data media 0…
-
1
votes2
answers312
viewsA: How to fill a column of a DF Pandas using, as a comparison, a specific column between this and another DF?
Importing the pandas import pandas as pd Loading the test files df = pd.read_csv('./df.csv') df2 = pd.read_csv('./df2.csv', sep = ';') df CNPJ DATA codprojeto 0 123 2020-12-02 00:00:00 UTC 0 1 123…
-
0
votes2
answers82
viewsA: Print letters that are outside of Collections. Counter
import string from collections import Counter Picking up the ascii characters asciiLetters = string.ascii_letters Creating a dictionary with key(letter) and value(0) dicionario = {key:0 for (key) in…
-
1
votes2
answers212
viewsA: Create date variable
You can use the data_range of pandas: import pandas as pd from datetime import date data_atual = date.today() datas = pd.date_range(start = '11/05/2020' , end = data_atual, freq='D')[::-1] The…
-
1
votes2
answers231
viewsA: download with requests and open python
Importing the required packages, please note that I have placed warnings as it shows a ssl error import requests import zipfile import io import warnings warnings.filterwarnings('ignore') File url…
-
0
votes1
answer192
viewsA: Python Api Data Filtering
Here you make the code of a json: dicionario = json.loads(requisicao.text) You can filter through the Keys of the dictionary: dicionario['USD']['name'] 'Dólar Comercial' dicionario['USD']['low']…
-
0
votes1
answer35
viewsA: How to change image name with python?
To do this you can use the code below: import os from shutil import move Defining the path and extension of files caminho = 'C:/caminho/para_as_imagens' ext = '.jpg' Taking the list of file names in…
pythonanswered lmonferrari 3,550 -
5
votes2
answers108
viewsA: Automate column subtraction in R
You can subtract the data frame by "moving" the "day/column" by making a Slice. Here we have the data frame (first slice) df[3:ncol(df)] `02/11/2020` `03/11/2020` `04/11/2020` `05/11/2020`…
ranswered lmonferrari 3,550 -
5
votes1
answer245
viewsA: Relative Frequency Table - R / R Studio (% Daily Sales Determined Date/Product)
Loading packages and xlsx: library(readxl) library(lubridate) df <- read_excel('./tempo_atendimento.xlsx') Making some conversions: df$COD_PRODUTO <- as.factor(df$COD_PRODUTO) df$RANGE_DIAS…
-
1
votes1
answer197
viewsA: Numeric types to Aggregate error
Your error occurs because you are trying to aggregate a string as if it were a numeric variable. You must turn the money column into numerical value: dados['money'] =…
-
4
votes3
answers95
viewsA: Date sequence from a range in R
Using dplyr and lubridate you can use rowwise that enables you to work line by line: library(dplyr) library(lubridate) nova_base <- base %>% rowwise() %>% do(data.frame(ID = .$ID, DATA =…
-
1
votes1
answer306
viewsA: Sqlite - Python insert data automatically
If using the pandas to store the database values, you can do as follows: Importing the necessary packages import pandas as pd import sqlite3 Simulating incoming data and creating a data frame…
-
1
votes1
answer36
viewsA: Doubt when counting table elements in Chr format
I believe you can solve using dplyr Count: flights %>% count(dest))
ranswered lmonferrari 3,550 -
2
votes2
answers285
viewsA: How to bring more fields in the Pandas groupby, without necessarily having to use them in the grouping?
One solution would be to create a 'filter': filtro = df.groupby('data')['contador'].max() And then use the isin of the pandas: df[df['contador'].isin(filtro)].reset_index(drop = True) Exit: data…
-
2
votes1
answer116
viewsA: Select python - Pandas
Importing the package import pandas as pd Loading the files: lojas = pd.read_excel('./lojas.xlsx') produtos = pd.read_excel('./produtos.xlsx') Using Jay of the Pandas: novo_df =…
-
0
votes1
answer45
viewsA: How to expand a dataframe based on a condition
importing the pandas package import pandas as pd Creating the data frame df = pd.DataFrame({ 'left_bound' : ['1', '4', '10', '25'], 'right_bound' : ['3', '9', '24', '50'], 'code' : ['a', 'b', 'c',…
-
5
votes1
answer45
viewsA: Randomizing two sets of numbers, not repeating the values within each group (R)
Data frame.: df <- data.frame(ID = c(1, 1, 1, 3, 3, 3, 7, 7, 7)) Dyplr package: library(dplyr) Separating the problem into 2. Here we create set1 grouped by ID: df <- df %>% group_by(ID)…
-
1
votes1
answer195
viewsA: Check if the content already exists in the text file
Sample tokens: tokens = [ 'Nzc3NzA2MTc2MzA4NzA3Mzc5.X7HVkg.v85rDccvWP-HJJxD_SMonOu', 'Nzc3NzA3ODI2Njg0MjMxNzEy.X7HXBw.CvxmjqeS8sW9Rx1sEy2ESLZ',…
-
0
votes1
answer39
viewsA: list with values greater than those reported by the user
Importing the package: import pandas as pd Loading the data and creating a new column: dados = dados = pd.read_csv('./DadosClimaticos2018Londrina.csv', sep = ';', parse_dates = ['Data'])…
-
1
votes2
answers150
viewsA: Cross-reference two different dataframes with different line numbers
You can use replace with a dictionary. Importing the package: import pandas as pd Creating the first data frame: Grau_Instr_Bibl = {'Categoria': ['Analfabeto', 'Até 5ª Incompleto', '5ª Completo…
-
1
votes1
answer145
viewsA: define average function with pandas
Importing the pandas: import pandas as pd Reading the data and storing in a variable parse_dates converts the Date column to datetime64 dados = pd.read_csv('./DadosClimaticos2018Londrina.csv', sep =…
-
0
votes1
answer323
viewsA: Create dataframe pandas by dicionario 1 key and 1 value
If you want to use the values as index and the words as values you can use the following code: import pandas as pd pd.DataFrame(list(dic.keys()), index = dic.values()) In the first parameter we pass…
-
0
votes1
answer74
viewsA: How do I access elements of a json that contains multiple keys in python?
One of the ways is to pass the Keys(keys) explicitly: import requests requests= requests.get("https://api.hgbrasil.com/finance/quotations?key={}") dados = requests.json() moeda = input("Moeda…
-
0
votes2
answers141
viewsA: Addition of columns in csv file - Python
You can do it this way: Creating the month column: dfdados['Mes'] = pd.DatetimeIndex(dfdados['Data']).month Saving the csv: dfdados.to_csv('./novo.csv',sep = ';', index = False) More about the…
-
3
votes5
answers1031
viewsA: Check that all items in the list are equal
You can make a function that checks if the next number is equal to the current number, if different already returns as soon as the list is not fully equal(False), otherwise if the for is until the…
-
1
votes2
answers266
viewsA: Receive two numbers, add the pairs and multiply the odd
User inserts start and end value: valor1 = int(input()) valor2 = int(input()) Stores the sum of pairs and the multiplication of impairments: par = 0 impar = 1 Here the loop for iterates over the…
-
1
votes2
answers47
viewsA: Increase the number of columns in the histogram
Utilize breaks: hist(histo$MCP, xlab= "MCP área (ha)", ylab = "Frequência", breaks = 5) I believe Bins is used in ggplot.
ranswered lmonferrari 3,550 -
0
votes1
answer68
viewsA: How do I get the input to be on the same line?
You can use map, split and multiple assignments. As shown in the example below: D, R, L, P, G = map(int, input().split()) Split: is a method that returns a list from a string. Map: executes a…
-
1
votes2
answers97
viewsA: How to access a list that is inside another list in Python?
You could otherwise store as dictionary for example, but follows a possible answer to list within list: for i, aluno in enumerate(listaAlunos[0]): print(aluno) print(f'nota 1 -…
-
0
votes1
answer203
viewsA: How to save requests return and save to . txt or . csv file in Python
If you want to save the file exactly as it is in the list: import io with io.open('arquivo.txt', "w", encoding="utf-8") as file: file.write(str(lista))
-
2
votes1
answer1640
viewsA: How can I make two graphs in the same Python Plot in Jupyter Notebook?
You can create the chart this way: import matplotlib.pyplot as plt from pandas import read_excel df = read_excel('./carmen.xlsx', names = ['A','B','C','D']) Defining what will be plotted:…
-
4
votes1
answer62
viewsA: Fill date.frame using for output
You can create the data frame this way, passing the range as value and as index: import pandas as pd x = pd.DataFrame({'v1':list(range(1,11))}, index = list(range(1,11))) x Exit: v1 1 1 2 2 3 3 4 4…
-
2
votes4
answers201
viewsA: How to reduce Python code 3?
An alternative with Join: n = int(input()) if n % 2 == 0: n +=1 ' '.join(str(v) for v in range(n, n + 12, 2)) or n = int(input()) ' '.join(str(v) for v in range(n, n + 12) if v % 2 == 1)…
-
2
votes1
answer219
viewsA: How to take only the time of a Timestamp
You can do with the time function: import pandas as pd a = pd.to_datetime(1490195805, unit='s') str(a.time()) Exit: '15:16:45'
-
1
votes2
answers258
viewsA: How to divide the value of an element of a column by delimiter (p.e "|") in pandas?
One way to do this is by using the function explode of own Pandas: df = pd.DataFrame(df['coluna1'].str.split('|').explode().reset_index(drop = True)) Entree: coluna1 0 ola|52 1 hey 2 sou 3 ja 4 da|5…
-
0
votes3
answers1399
viewsA: Python Range - Numbers in descending order
The way you built the algorithm you need to get 2 from the start of the range(start, stop, step): numero = int(input('Informe um número inteiro positivo e par: ')) while numero <= 0 or numero % 2…
pythonanswered lmonferrari 3,550 -
7
votes3
answers71
viewsA: Conditional column based on multiple dplyr lines
I used the group_by + mutate + case_when + all to verify that all occurrences of the determined id were yes/no and those mixed would be missing values and filled with nd. library(dplyr) df %>%…