I have a list of “states” from which I have to iterate: states = [‘antioquia’, ‘boyaca’, ‘cordoba’, ‘choco’] I have to iterate one column in a pandas df to replace or cut the string where the state text is found, so I try: And the result is: Result w…
Tag: python
DRF – Relate an object to another using views, serializers and foreign key
Basically I have two models with one-to-one relationship. And I want retrieve information from one, when I call the other. My models: So I can just call an POST and create an Customer, that customer will have id, first_name and last_name. For now everything is working. But when I try to call my endpoint to cr…
How to make menu using dict evaluation for small python guessing game
So let’s say if user inputs 1 in menu screen, I want to call start_game() function, I know there is other way to do this but just interested in this particular way because I think its neat. Answer Don’t put strings in the dictionary, put references to the function. Then you can do: to execute the …
How to exclude Optional unset values from a Pydantic model using FastAPI?
I have this model: So I want to be able to take requests like: and Right now in the second case I get using this code: So how can I exclude n_processes here? Answer You can use exclude_none param of Pydantic’s model.dict(…): Output: Also, it’s more idiomatic to write Optional[int] instead of…
Share RLock between multiple instances of Python with multiprocessing
Consider this MWE: When this script is executed, it instantiates the_setup and serves it. Then I want clients to be able to do things like this from other scripts: However, I get RuntimeError: RLock objects should only be shared between processes through inheritance. If the with the_setup.hold_hardware(): is …
Adding json value in yml in python changing the format to yml again
I have to fetch json data and paste it in one of the values in yml. But it is converting it to yml format. I want the output as below but with the above script it is showing all the data in dictionary format format. I would like to replace the sampleyaml with the added changes. Please help me. Answer
convert multiple units to KG in pandas
let’s say i have this code(which is obviously wrong) I want to apply such condition to each value in a column(weights) based on another column(weight unit). Is there an efficient way to do it. Preferably allowing a func pass so easy to modify Answer Don’t use a function, this will be slow. numpy.v…
if statement for columns in 2D list
I need to iterate two different lists of integers with an if-statement that requires a mathematic operation. I’ve approached this in several ways, the latest one shown below. I have 4 lists that I’ve extracted from columns in a CSV file: I need to identify the names for those competitors who are o…
Regex Match on String (DOI)
Hi I’m struggling to understand why my Regex isn’t working. I have URL’s that have DOI’s on them like so: And I’m using for example this Regex, but it always returns empty? Where have I gone wrong? Answer It looks like you come from another programming language that has the notio…
expand row based on integer in column and split into number of months between dates
I have the following dataframe: id date_start date_end reporting_month reporting_month_number months_length 1 2022-03-31 23:56:22 2022-05-01 23:56:22 2022-03 1 3 2 2022-03-31 23:48:48 2022-06-01 23:48:48 2022-03 1 4 3 2022-03-31 23:47:36 2022-08-01 23:47:36 2022-03 1 6 I would like to split each id row so I c…