Suppose I have a table like this Company_Name Product A Apple B Orange C Pear D Lemon Given two lists list1 = [‘Pear’, ‘Lemon’, ‘Apple’, ‘Orange’] list2 = [1, 2, 3, 4] How to replace Product name with the numerical values? The output should look like this –…
Tag: python
Gensim Word2Vec exhausting iterable
I’m getting the following prompt when calling model.train() from gensim word2vec The only solutions I found on my search for an answer point to the itarable vs iterator difference, and at this point, I tried everything I could to solve this on my own, currently, my code looks like this: The corpus varia…
how do I create a function that randomly replaces vowels in a string with * in python
Answer Answered according to the comment clarification: pick a random vowel, replace all occurences of this vowel. random.choice picks a random element from a sequence. Strings are also sequences, and “aeiou” looks prettier (and is slightly, very slightly more memory-efficient, though this is a no…
I am stuck with this for loop problem in python [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 months ago. Improve this question I am trying to make a a program to calculate the probability of a game. So the game is like…
Pandas apply condition on a column that contains list
I want to create a new column based on a condition that have to be applied on a list. Here’s a reproducible example: As one can see, each object in the BRAND column is a list that can contain one or more elements (the list can also be empty as for the row where ID = 1). Now, given the
Inserting values into JS scripts using python
I am parsing JS files stored in a directory to fetch out the values inside tags. Upon finding relevant values in the tags using regex, I want to move those values under a new section called controls. I have been able to get the required values in the controls variable using the script below: I am not able to …
Optimising Python script for scraping to avoid getting blocked/ draining resources
I have a fairly basic Python script that scrapes a property website, and stores the address and price in a csv file. There are over 5000 listings to go through but I find my current code times out after a while (about 2000 listings) and the console shows 302 and CORS policy errors. As you can see I added slee…
TypeError: list indices must be integers or slices, not str. Python
So, I have this piece of code: It gives me this response: Then I have this piece of code, that should give me the page I need: But I get this: I tried a few other ways to do this: But nothing works Answer You’re trying to retrieve a dictionary item with a list as a value. json.loads(grade.content)[̵…
Visually see edges and node color in networkx graph
I am creating a graph that will represent a Fat Tree (https://www.researchgate.net/figure/Fat-Tree-structure-with-n-4-It-has-three-levels-of-switches_fig1_220429211). Currently, when I visually see the network I code up, the nodes are close together but I do not see a visible edge between them. How do I make …
Filter and apply multiple conditions between multiple rows
I have the following dataframe: What I’m trying to do, is to implement the following logic: If there’s only one record (consider the combination of location + method), I’ll just do nothing. That’s the scenario for the first and last row. If there’s more than one record (location …