returns n smaller indexes per column using pandas

I have the following (simplified) dataframe: df = pd.DataFrame({'X': [1, 2, 3, 4, 5,6,7,8,9,10], 'Y': [10,20,30,40,50,-10,-20,-30,-40,-50], 'Z': [20,18,16,14,12,10,8,6,4,2]},index=list('ABCDEFGHIJ')) Which gives the following: X Y Z A 1 10 20 B 2 20

how to add a numpy matrix in an empty array

I want to append a numpy array(matrix) into an array through a loop data=[[2 2 2] [3 3 3]] Weights=[[4 4 4] [4 4 4] [4 4 4]] All=np.array([]) for i in data: #i=[2 2 2 ] #for example h=i*Weights #h=[[8 8 8][8 8 8][8 8 8]] All=np.concatenate((All,h),ax

Is it possible to prioritize a lock?

I have a multiprocessing program where one process adds elements to a shared list (multiprocessing.Manager().list()) several other processes consume these elements from that list (and remove them); they run until there is something to process in the

Python CSV formatting with string and float and write

How do you format a float to 2 decimal points with mixed data type? I'm fetching a table and writing rows to csv file. My data is (string, string, float, float, float...) sql = 'select * from testTable' c.execute(sql) columnNames = list(map(lambda x:

split the text list into nGrams into Python

I have to split a text file into a specific amount of words per list in list, probably be best to show in example. say the text file looks like this "i am having a good day today" i have to write a function which looks like this ngrams.makeNGram

how to count elements / boolean after if-statements (python)

I have a csv where I need to count the total number for each category: New_Cell[2] Category I need to count 1 [A01] 1 [A01] 0 [A01] 1 [A01] 0 [A02] 1 [A02] 1 [A02] 2 [A02] 1 [A02] I need it to count each occurrence of TRUE for the if-condition so tha

Keyword Functions for Python min / max

I am trying to understand how this works: my_dict = {'a':2,'b':1} min(my_dict, key=my_dict.get) produces b Which is a really cool feature and one I want to understand better. Based on the documentation min(iterable[, key]) Return the smallest item in

Reading a csv file with a timestamp column, with pandas

When doing: import pandas x = pandas.read_csv('data.csv', parse_dates=True, index_col='DateTime', names=['DateTime', 'X'], header=None, sep=';') with this data.csv file: 1449054136.83;15.31 1449054137.43;16.19 1449054138.04;19.22 1449054138.65;15.12

how to divide a list into different python strings

i have a list object like this value = [<employee 'Mark Twain' as 'Captain'>,<employee 'Huckle' as 'Cowboy'>] now i would like to have the employee and in a seperate variable like emplo[0] and as[0] like emplo[0] = 'Mark Twain' as[0] = 'Captai

OpenCV - Access to variables cv2.findContours

I am using Opencv 3.0 with Python 2.7, for an Optical Character Recognition I need to find points of each contours that are found by findContours method. When I do debugging, I can see there are 208 countours found but I am curious about how can I ac

why my python code does not encrypt or decrypt my message

For some reason when I run the code at the end it just displays the message without encrypting or decrypting it im really confused please do not hate on me if this is really obvious I am very new to python and barely know the basics #declare variable

Waiting for a table to fully load using selenium with python

I want to scrape some data from a page which is in a table. So I am only bothered about the data in the table. Earlier I was using Mechanize, but I found sometimes some of the data are missing, especially in the bottom of the table. Googling, I found

ImportError: No module named VIM site and Enthought Canopy

I was using vim and python perfectly fine till I upgrade to Maverick. Now I have a similar issue as here. When I remove remove canopy path from bash_profile and use default python path , vim works fine, otherwise I am getting the above error. You can

List identified with another, using new elements in the loop

This question already has an answer here: python list by value not by reference 11 answers I have a list named MyList. I want to copy the list to a new one, then add items to the new one, so I do: MySecondList=MyList for item in MyList: if item==2: M

Delete a group after pandas groupby

Is it possible to delete a group (by group name) from a groupby object in pandas? That is, after performing a groupby, delete a resulting group based on its name.Filtering a DataFrame groupwise has been discussed. And a future release of pandas may i

Python: How to select all other elements in a for loop

Let's say I have: def recursive(data): for i in data: if condition: recursive(HERE IS THE ISSUE) How do I pass in the data from the current i to the final element in the list?Check out enumerate this will also return the current loop index which you

Django Abstract Pattern Inheritance

In a model I usually put a "uuid" field for friendly URI, also a "slug" field. Say I have a model named "SomeModel", by overriding its save() method, I can generate a uuid and a slug when it's being saved: class SomeModel(mod

Strip in Python

I have a question regarding strip() in Python. I am trying to strip a semi-colon from a string, I know how to do this when the semi-colon is at the end of the string, but how would I do it if it is not the last element, but say the second to last ele

CherryPy with additional threads for custom tasks

We're designing a system based on CherryPy that in addition to serving web requests needs to do tasks/jobs in parallel. We want it to be a single process running as a daemon and create threads for all the parallel jobs like scheduled tasks or collect

Sending 2 forms at a time

I must submit two forms at once and I don't want javascript. So I think I can post the form to myself, do my work and then post it on to the third-party payment provider. This is to make the order appear in our datastore. The manual says how to submi

Get a pyOpenSSL Client to Use SSL Session Summary

I've been trying with no success to get my pyOpenSSL client to use TLS/SSL session resume when making several connections sucessively (it's sending http requests) to a Tomcat application server. I'm pretty sure everything is fine on the server end si

How to turn a dictionary into a string?

params = {'fruit':'orange', 'color':'red', 'size':'5'} How can I turn that into a string: fruit=orange&color=red&size=5 You can do it like this: '&'.join('%s=%s' % (k,v) for k,v in params.items()) If you are building strings for a URL it would

How to find and replace special characters utf-8 in Python?

I'm a Python beginner, and I have a utf-8 problem. I have a utf-8 string and I would like to replace all german umlauts with ASCII replacements (in German, u-umlaut 'ΓΌ' may be rewritten as 'ue'). u-umlaut has unicode code point 252, so I tried this:

py2exe + sqlalchemy + sqlite problem

I am playing around with getting some basic stuff to work in Python before i go into full speed dev mode. Here are the specifics: Python 2.5.4 PyQt4 4.4.3 SqlAlchemy 0.5.2 py2exe 0.6.9 setuptools 0.6c9 pysqlite 2.5.1 from distutils.core imp

Project idea in Python or C #

So far I was familiar with c and c++ only, and I have programmed in these languages only. I have some experience working on some projects in c or c++. Some good projects or applications I've made till now: Duplicate file finder and remover Process mo