Python: Front fills and zeros

Suppose I have a dataframe, df1, that has zeros and nans: dates = pd.date_range('20170101',periods=20) df1 = pd.DataFrame(np.random.randint(10,size=(20,3)),index=dates,columns=['foo','bar','see']) df1.iloc[3:12,0] = np.nan df1.iloc[6:17,1] = 0 What's

Threading the problem in Python 2.7

import time import thread import termios import sys import datetime try: from msvcrt import getch # try to import Windows version except ImportError: def getch(): # define non-Windows version import tty, termios fd = sys.stdin.fileno() old_settings =

if / else statements vs dictionary

If I were to write a statement using dictionary instead of an else statement, how would that be? For example, say I have def determineRank(years): if years == 1: return "Freshman" elif years == 2: return "Sophmore" elif years == 3: ret

Binary search of a string in a string list

I have a list of permutations of a string, and a list full of words from a lexicon. I want to for each permutation find out if it's in the list of words. I tried a while loop and just brute-forced through and that gave me a bunch of words from the wo

index () of the last occurrence in the python list

This question already has an answer here: How to find the last occurrence of an item in a Python list 9 answers In my for loop, I got this code: start = listData.index(datum) + 1 and listData is: listData = ['H66', 'B35', 'L21', 'B35', 'H66', 'J02',

Fill a dictionary in a dictionary

I have 2 lists: num = ['10', '10', '10', '88', '77', '77', '10'] typ = ['KB', 'BK', 'KB', 'TP', 'HK', 'KH', 'KB'] I want to make a dictionary of dictionaries that will look like this: {'10':{'KB':3, 'BK':1}, '88':{'TP':1}, '77':{'HK':1, 'KH':1}} I wi

How do I write a new line or \ n to a file

I have a simple code where i wanted to write 2 files into 1. I wanted to write a new line or '\n' after the first file is written. I tried using files.write but unable to do it. Can someone help me in this? This is my code: files = run('ls -ltr /opt/

Multiply specific columns of data by constant scalar value

How do I multiply only specific columns of a dataframe by a constant value? df0 = pd.DataFrame({'A' : 1., 'B' : 1, 'C' : 1, 'D' : np.array([1] * 4,dtype='int32')}) mult_by_two = df0.iloc[:,2:].mul(2) print mult_by_two I get this: C D 0 2 2 1 2 2 2 2

Azure Environment Variables in Python Cloud Service

I have a Python Cloud Service running on Microsoft Azure which should be using a different Storage Account (for blob storage and queues) when it's running in dev vs. staging vs. production. I'd rather not hard-code the Storage Account credentials, bu

The Python class does not work after copying

I copied some code from nltk directly into my project (with crediting sources) because my school computers don't allow me to install libraries. I copied the PorterStemmer class and StemmerI interface from here However, when I run the code, I get NotI

first loop add-in program

I am trying to write a program which determines the sum of the first 1000 prime numbers. def main(): sum = 0 numberOfPrimes = 0 divcount = 0 for number in range(1,10000): for divider in range (1,(number/2) + 1): if number%divider == 0: divcount += 1

How can I install Hadoop and Pydoop on a new Ubuntu instance

Most of the setup instructions I see are verbose. Is there a near script-like set of commands that we can just execute to set up Hadoop and Pydoop on an Ubuntu instance on Amazon EC2?Another solution would be to use Juju (Ubuntu's service orchestrati

Did Python3 typecast enter the input () value?

I am writing a raingauge precipitation calculator based in the radius of the raingauge. When I run my script, I have this error message: Type de raingauge radius [cm]: 5.0 Traceback (most recent call last): File "", line 27, in <

How to generate output as sound in python ...?

I am generating random number in python as below, print random.randrange(100, 1000, 2) Is there any way to convert that output to sound(audio) format..?I am not sure what you are trying to achieve. I assume you are trying to generate a random number

Parsing GenBank file: get the locus tag vs. the product

Basically, a GenBank file consists on gene entries (announced by 'gene' followed by its corresponding 'CDS' entry (only one per gene) like the two I show here below. I would like to get locus_tag vs product in a tab-delimited two column file. 'gene'

Keep the live audio stream synchronized

I'm working on an application to provide multi-room audio to devices and have succeeded in keeping audio playing from a file (e.g. mp3) synced using GST and manually using NTP but I can't seem to get a live audio stream to sync. Essentially I want to

Google Calendar - Python - Synchronization

I've been researching and having a hard time trying to figure out how to work my solution out. Basically my end goal is to be able to log into Google Calendar and grab a dates events and work with the information. If possible, be able to use multiple

Password protects an entire django application

I am running a simple staging env on heroku and I am now looking to password protect the whole app with some sort of simple authentication I am wondering if there is a simple app or middleware that already supports this. Have tried looking around for

Xml to python processing options

I am currently writing a non-web program using mostly python and have gotten to the point where I need to create a structure for save and settings files. I decided to use xml over creating my own format but have come into a bit of a barrier when tryi

Shape and shape processing tools for use with twisted.web

As the title states, I am looking for something, that will help me automate form processing (validation/rendering/etc) in twisted.web. I am also looking for a suitable templating toolkit to use with it. As for templating, it is not so much of an issu

Best Practice in Python for Return Value on Error or Success

In general, let's say you have a method like the below. def intersect_two_lists(self, list1, list2): if not list1: self.trap_error("union_two_lists: list1 must not be empty.") return False if not list2: self.trap_error("union_two_lists: lis