I want to get a common library from several files. In order to reduce the amount of code, I want to create and manage a separate file for import. Will it be possible? import_lib.py other_1.py other_2.py Answer Don’t try to hide the imports needed in your module in another module – it’ll make…
Tag: python
How can I check the value that exists for the earliest date?
df: country year index Turkiye 1992 NaN Spain 1992 NaN US 1992 1 Turkiye 1993 1 Spain 1993 1 US 1993 0 Turkiye 1994 1 France 1994 0 Italy 1994 NaN Turkiye 1995 0 Here, for example, in 1992 Turkiye and Spain are NaNs but the index exists for the US. So I am only interested in the earliest date
Calculator not functioning properly
Hey I’m trying to make a small calculator for a project, however I’m struggling to get the code to work. When I try to run operation 4, I want the code to ask if you would like to run another calculation, if you answer with ‘yes’ it will repeat the statement again. However all it does …
Add a space after a word if it’s at the beginning of a string or if it’s after one or more spaces, and at the same time it must be at end or before n
How to obtain this outputs from those inputs? Note that for examples 4, 5, 6 and 7 the regex should not make any changes, since after the word there is already a space placed, or because in the case of “uno”, the word “un” is not at the end of the sentence, or in the case of “tre…
Accessing a parent model attribute within Django template
I have an app that has a list of projects, and each project has a number of posts, and when you click on a project, it shows all of the posts in that project, but I can’t figure out how to make it display what project the user is currently on, sample of it not working It should say “Posts
Invoking python3 with no arguments results in the interpreter opening a script called dis.py in the current directory. How to avoid similar problems?
Invoking the python 3.10.6 interpreter with no arguments produces the following output in the presence of a (possibly empty) file called dis.py in the working directory. Clearly I shouldn’t have my own files called dis.py lying around in the working directory! Are the names of all of the other modules i…
How do I add the counts of two rows where the values in the columns are swapped with respect of the other?
I have a Dataframe as follows: I have to count how many pairs of Sources and Targets there are. (1,0) and (0,1) will be treated as duplicate, hence the count will be 2. I need to do it several times as I have 79 nodes in total. Any help will be much appreciated. Answer To count pairs regardless of their
Test (unittest, mock) if a global variable list is updated
file.py: test.py: I always get an AssertionError. from the line assert x in GLOB_VAR Let me say that I DO need a global variable Answer It turned out that I shouldn’t have patched the global variable as I need to assert against the file’s global variable and not against the a mock’s instance…
Json to CSV file conversion using python
I have a sample JSON file like this i need to take only specific key and values from the json file and convert it into a CSV file Code: Ref:Extracting Specific Keys/Values From A Messed-Up JSON File (Python) When I’m passing 2 values through the extract(), getting NaN in between result I need to get an …
Passing multiple dataframes as arguments to a function
I have been scratching my head since morning on how to tackle this problem. I need to pass multiple columns from multiple dataframes to a function as arguments. Example : Df1 A B C 1 11 111,333 2 22 222 3 33 nan Df2 D E a 111 b 333 Now, I want all the rows from Df2(col E) which