Combine multiple data side by side and rename them

I have multiple data frames in my analysis. For example dataframe 1 where this is the number of people by activity in China Activity No of people Activity 1 100 Activity 2 200 Activity 3 300 and data frame 2 where this is the number of people by acti

Pandas extract rows according to complex conditions

I have this dataframe: source target weight 24517 class social 31 24356 class proletariat 29 16189 bourgeoisi class 29 24519 class societi 29 24710 class work 28 15375 bourgeoisi class 26 23724 class condit 24 24314 class polit 24 ... How can I creat

Returns the list of items that exceed all previous items

I am new to Python, can anyone please point out the mistake, i have made in the code: numlist = [3,2,5,5,7,6,1,8,4] def peaks(numlist): exceed = [] for elem in numlist: for num in exceed: if elem not in exceed: if num in exceed < elem in numlist: exc

python indexing and affect by ndarray line

Something is wrong in my script and I found the error, but I'm completely stuck. there is array b which contains two elements: b = np.zeros ((1,2)) b[0,0] = 272 b[0,1] = 1578 I want to check if there are elements in the second columns, greater than a

list [-1:] returns the last value, but is not == last value?

I'm still very new to coding, as in I've been coding for a few days. I'm trying to teach myself python by working on a small personal project. I've been playing around with list slicing, and something doesn't add up to me, which is impeding a functio

to remove duplicate strings in a python list

This question already has an answer here: How do you remove duplicates from a list in whilst preserving order? 33 answers I have a problem in my code where I have the following: import random Probabilities={'AA':0.2,"TT":0.2, "GG":0.1,

How to modify the elements of a list in the list

I am very new to Python, trying to learn the basics. Have a doubt about the list. Have a list: L = [[1,2,3],[4,5,6],[3,4,6]] The output should be: [[2,4,6],[8,10,12],[6,8,12]] The code that works for me is the following for x in range(len(L)): for y

Regex to find suffixes in names

I'm having trouble writing a regular expression that would find and remove suffixes in names in strings. I had it working fine just looking for the suffix (e.g. 'III', 'IV'), until I had names with IV start appearing. I know the logic once I get the

Slow Python Search Loop

I am running a search on a list of ads (adscrape). Each ad is a dict within adscrape (e.g. ad below). It searches through a list of IDs (database_ids) which could be between 200,000 - 1,000,000 items long. I want to find any ads in adscrape that don'

Journaling children in a supervisor subprocess

My system is composed of a python application that is launched from supervisord. Let's call it A. A launches a subprocess B to do some of its task. Both A and B are coded in Python and use the standard logging module to output messages to the console

I want to write to a new file not add for each iteration

I want to create a new file for each iteration, but I'm not able to do so. I'm only able to update the same old file, how could I solve this? The other more basic question what is "" function in file.write("".)? I have tried with "

a Django URLField set max_length to 200 characters

link = models.URLField(max_length=500, blank=True, default='') I have already set the max_length as 500, but every time I tried to set this field with url larger than 200 characters, it still threw an error saying that Ensure this value has at most 2

How to broadcast an openCV video on an HTML webpage?

I am making a robot that will have a webcam on it to provide some simple object detection. For right now, I would like to simply stream the video to a webpage hosted on the robot and be able to view it from another device. I have written a simple tes

How to encode an integer in a base64 string in python 3

This question already has an answer here: Why do I need 'b' to encode a Python string with Base64? 5 answers I'm trying to encode an int in to base64, i'm doing that: foo = 1 base64.b64encode(bytes(foo)) expected output: 'MQ==' given output: b'AA=='

Python index error while trying to use list.insert

Here is a section (a large section) of my code. What i have happening, by design, is to iterate through this sequence until the distance that i am calculating is minimized by 1cm. I don't want to move on to my next epoch

Problem with the evaluation of an input sentence in python

I am in a class teaching python and am a beginner at any sort of coding. I keep running into this problem and I can't find anything in my text book or additional handouts explaining what I'm doing wrong. Here is an example taken from one of the exerc

Python-mysql: When to explicitly cancel a transaction

Suppose, I have a modifying statement: cursor = conn.cursor() # some code affected_rows1 = cursor.execute(update_statement1, params1) # some code conn.commit() cursor.close() Should I wrap the block of code with a try ... except and explicitly rollba

Run the Python script as root (seteuid vs c-wrapper)

I have a quick one off task in a python script that I'd like to call from Django (www user), that's going to need to root privileges. At first I thought I would could use Python's os.seteuid() and set the setuid bit on the script, but then I realized

Interactive graphic visualization

Situation Similar to this question, I'm looking for a way to create a GUI where users are able to see a graph (in the graph theory sense) and interact with it. Vehicles will move across the graph from none to node over time. Users should be able to a

Python: Difference between add and __add__

In Python, what is the difference between add and __add__ methods?A method called add is just that - a method with that name. It has no special meaning whatsoever to the language or the interpreter. The only other thing that could be said about it is

How can I print only all three indexes in Perl or Python?

How can I do a for() or foreach() loop in Python and Perl, respectively, that only prints every third index? I need to move every third index to a new array.Python print list[::3] # print it newlist = list[::3] # copy it Perl for ($i = 0; $i < @list;