Posts by Lucas • 3,858 points
153 posts
-
6
votes2
answers187
viewsQ: How does regular expression with double range work?
I am studying regex and had a doubt when dealing with the range operator (-). I understand a creak like [a-z] means "the whole alphabet in lowercase letters" and that a range like [^A-R]it would be…
-
3
votes1
answer158
viewsA: Why use double clasps in Pandas?
In practice, they are two equal operators and are equivalent to the method __getitem__, according to the documentation of the pandas that you can see here. It is easier to understand this…
-
4
votes2
answers1707
viewsA: Brazilian calendars using Pandas-python
First, it is good to check if there is a method specifically for Brazil in this module. To check the methods of this and any other module, use the function builtin dir(). So we have: from…
-
1
votes2
answers384
viewsA: Download data from Stock Exchange tables in R
It is easy to notice that there is no error in your code. To see this, just replace your url with a url that has a well formatted table. I use this as an example website. Behold: library(tidyverse)…
-
5
votes1
answer77
viewsA: How to create one vector from another, but its elements must be greater than x?
Without appealing to a ready-made function, you can simply use a conditional within a for loop as in the example below: v1=c(1500,1600,1700,1800) v2=c() for (i in v1) { if (i>1600) { v2=c(v2,i) }…
-
0
votes1
answer58
viewsQ: How to make the number of executions of an algorithm a function of processing capacity and not of time?
I wrote a little program to send a message on my father’s Whatsapp to remind him to take his meds. Below I present the code that can be replicated by anyone with a mobile number and a record in the…
-
2
votes1
answer49
viewsA: Problem reading file and turns it into a key and value dictionary
Well, I guess just make an iteration using the readlinesas an iterator. Behold: import re regexp = '(\]\ \[).*(\]\))' pontuacao = '(\[-?\d*\.\d*\/)' status = '(\:\s.\s\(.*\)\:\s)' _id =…
-
4
votes0
answers379
viewsQ: How to change document language in rmarkdown using latex?
I’m writing a document in Rmarkdown and using Latex for figures and tables. I need the titles of figures and tables in Portuguese, so I tried the following: Rmarkdown file: --- Output: pdf_document:…
-
0
votes1
answer455
viewsQ: Problem turning string into int: "error: no matching Function for call to stoi"
I’m writing a little show input a string of integer numbers as a string and as output an array with twice each of the numbers, also as a string. Despite the function atoi be indicated in that reply,…
-
1
votes1
answer184
views -
1
votes1
answer184
views -
1
votes1
answer510
viewsQ: How to change location buttons dynamically using Tkinter?
I know that this code has a lot of repetition and I intend to "dry" it later. For now, I want to understand how to change the position of the buttons in Tkinter dynamically. The solution I initially…
-
0
votes1
answer85
viewsA: How to install C++ modules
Most packages and libraries do not need to be downloaded and installed manually on Ubuntu. You can install libfmt-dev using the official "Universe" repository, using apt. Run the following commands…
-
0
votes1
answer85
viewsQ: How to install C++ modules
I would like to know how to install new modules in C++. For example, I recently discovered that there is a module that has an application of the method format of strings very similar to the method…
-
2
votes2
answers93
viewsA: How to create multiple columns using the values of one in pandas?
I present below my solution. It is a particular implementation of a more general principle that can be used to implement a simpler solution. The principle is: create lists with the values of each…
-
2
votes2
answers586
viewsA: How to count repeated elements in a list of tuples?
You don’t need to use the entire list within the for loop since you only need the first element of the tuples. A solution would be to create a sub-list from the first and only then iterate. See:…
-
4
votes1
answer217
viewsQ: How to set default in parameters when specifying the type of the object?
When defining functions, I usually use the feature of specifying the type of object that can be used as argument and which object the function should return. See example below: def…
-
0
votes1
answer163
viewsQ: How to use user-clicked button text as input in Tkinter?
I would like to use the text of the button clicked by the user of my game to make a logical test and return another question to the user. The game will work with a sequence of these questions, and…
-
2
votes1
answer74
viewsQ: How do I build attributes based on values defined in the __init__ method in my class?
I wrote the following class: class Words(): def __init__(self, value, g_class, first_meaning = True): self.value = value self.g_class = g_class self.first_meaning = first_meaning #attribute meaning…
-
2
votes3
answers2843
viewsA: How does train_test_split work in Scikit Learn?
Well, to understand this question, it is illustrative to start by presenting the conceptual difference between models of Machine Learning supervised and unsupervised. Starting with the latter,…
-
2
votes1
answer1805
viewsA: How does pivot table work on pandas?
There are two ways to pivot in the pandas, the pivot and the pivot_table. Both the pivot as to pivot_table return a dataframe as output, the difference between them is that the first does not accept…
-
1
votes3
answers2524
viewsA: Divide date (day, month, year) into new columns - Dataframe Pandas
You can use lambda to turn the date variable into string and then use slicing to select the part of the date you want. In your case: df['ano'] = df['data'].apply(lambda x: str(x)[:4]) df['mes'] =…
-
5
votes6
answers19845
viewsA: How to change the name of the pandas dataframe column?
You can pass a list with the new names you want: df.columns = ['nome_completo', 'idade'] Just an addendum, the command df.rename(columns = {'nome':'nome_completo'}) is correct, but for the change to…
-
1
votes1
answer602
viewsA: How to sort a column in the pivot_table Pandas
The output of the method pivot_table is a Dataframe, as can be seen in documentation. It is therefore sufficient to use the DataFrame sort_values. In your case: impute_grps.sort_values(by =…
-
2
votes2
answers175
viewsA: Import into different directories in Python3
You can simply name the directories separated by . when importing. For example, if I am working in the directory dir0 and if my package mod.py is in dir0/dir1/dir2/mod.py, can do: import…
python-3.xanswered Lucas 3,858 -
1
votes3
answers222
viewsA: re.error: bad escape c at position 0
The problem was in the exhaust caracteter \, without it your code runs normally, but does not execute what you want. Using the function zip it will not be possible to test for all entries of the…
-
1
votes1
answer106
viewsA: Typeerror: can’t pickle _thread. _local Objects when I try to use scikit-Learn RFE in a template created in tensorflow
Your code is correct, but it is not running and will not run because there is an incompatibility between the Keras and the function RFE (recursive Feature elimination ) do sklearn. As can be seen in…
-
3
votes1
answer732
viewsA: Loop to run python scripts
You can simply run them on the terminal inside a file bash. To do this, create an extension file sh and include commands to run the extension files py one after the other. Finally, go to the…
-
0
votes1
answer226
viewsA: What is the equivalent of Seaborn’s```argument in matplotlib?
The solution is the method legend_elements(), see documentation here. Rewriting the code with matplotlib, we have: import matplotlib.pyplot as plt import random import pandas as pd import numpy as…
-
1
votes1
answer226
viewsQ: What is the equivalent of Seaborn’s```argument in matplotlib?
When I want the points belonging to different categories to be colored each of a color in the seaborn, i simply put the categorical variable of interest as argument value hue. Replicable example:…
-
1
votes1
answer388
viewsA: How to loop python code, unittest, Selenium?
Well, I think your code has a lot of information unrelated to your problem. So I decided to make a minimal replicable example. Run this code on your computer, see how it works and apply to your…
-
1
votes1
answer138
viewsA: Multiply column with Manytomany Count() from the same Model Django
All you need is a function that performs the weighted sum of scores. Unlike what I initially thought, this function needs to be an accepted method in aggregate, then external functions do not solve…
-
1
votes1
answer303
viewsA: Using resample on pandas with intermittent date variable
Well, the solution was just to do: print(df.resample('3A').sum()) Within the documentation I quoted in the question, there was a link in a note containing the table below offset_string. So, you just…
-
1
votes1
answer303
viewsQ: Using resample on pandas with intermittent date variable
I have a database in which one column is the year and the other columns are the total of formal workers in a city (each column a city). My goal is simply to aggregate my annual data to triennials.…
-
1
votes2
answers107
viewsA: How to partition people in a network of friends?
In fact, just change the while for if since your logical test is excluding. See: def redes(n,lista_tuplas): rede = [] rede2 =[] for i in range(len(lista_tuplas) - 1): if…
-
0
votes2
answers53
viewsA: saving Dict list on file
Well, one possible solution is to use the Garbage collector to take the dictionary of each instance of your class, add these dictionaries to a list and save the list with pickle. See: import gc…
-
0
votes1
answer53
viewsA: How to make the Join() method work for each word from multiple lists within another list
The problem is your last two for loops. You can do what you want by changing the loops, using lists and the method insert of lists. See: par = [] impar = [] a = "" b = "" while True:…
-
1
votes3
answers344
viewsA: How to read a text file and generate a dictionary?
Beyond the character \t, there are excess empty spaces in the txt file. A combination of string methods can be used strip() and replace(). Before displaying the code, I’ll give you a brief example…
-
2
votes2
answers38
viewsA: Deleting item from a list without knowing its index - Python
You can identify the item you are interested in by the index and use the Try statement to deal with the error that arises when he does not find the item in the sublist. See: lista_de_dados =…
-
1
votes1
answer25
viewsA: Trying to lambda the dataset?
What you want is to make one subset bank. In this case you do not need to define a function. The correct command to find the number outliers of a variable var would be: import numpy as np Q1 =…
-
3
votes1
answer1343
viewsA: The Truth value of a Series is ambiguous. Use a.Empty, a.bool(), a.item(), a.any() or a.all()
The mistake "The Truth value of a Series is ambiguous. Use a.Empty, a.bool(), a.item(), a.any() or a.all()" occurs when python cannot make a comparison because the logical test is unclear. For…
-
1
votes1
answer76
viewsA: Is there any way to replace one number with another in a Dataframe?
replace has to be done with a dictionary. df['variavel'].replace({old_value : new_value}) Replicable example: import pandas as pd df = pd.DataFrame({'A': [0, 1, 2, 3, 4], 'B': [5, 6, 7, 8, 9], 'C':…
-
1
votes1
answer503
viewsA: Valueerror: Math Domain error
Well, as commented by another user, the function math.sqrtis only set to positive numbers and therefore returns an error every time delta is negative. There are two solutions to your problem. The…
-
0
votes1
answer107
viewsQ: Import user-created modules in javascript
I read in that reply here on the site I can import modules in javascript similar to what is done in python. However, I am not able to apply this in JS. Follow an example. In python varies: #arquivo…
-
1
votes1
answer65
viewsQ: Array changing of order
I’m new to javascript and I’m making a little game to learn the language. The idea is simply to ask the user to tell the sequence of the pi number and, in case of error, stop the program and give a…
-
0
votes1
answer335
viewsA: String handling with python and pandas
You can use the method replace of strings to replace the characters you want. To use string methods in pandas just apply the method str in the interest column. Example removing ?:…
-
1
votes3
answers73
viewsA: Key-value dictionary
You cannot have two keys with the same name. If you want to add cases with keys different you can use the method update: my_dict = {'exemplo1':1, 'exemplo2':2, 'exemplo3':3}…
-
0
votes2
answers1584
viewsA: How to insert data from one dataframe into an index present in another?
Merge, reset the index and then do the new index: new = df1.join(df2) new.reset_index(inplace = True) new.set_index('index', inplace = True) Just to clarify, df.append appends the databases. That…
-
1
votes2
answers1413
viewsA: How to turn list into array
EDIT: The previous version of this answer used the pandas' exploding method. It is also a possible solution, but the solution below, using to_list, is much simpler You can put these lists in a…
-
1
votes1
answer204
viewsA: error in a real-to-dollar python converter exercise
The problem is not the numerical conversion, but the fact that you are calculating the leftover as the rest of the division of real by dollar. The correct procedure is to subtract from the real…