Skip to content

Tag: python

A single string in single quotes with PyYAML

When I edit a YAML file in Python with PyYAML, all of my string values are saved back to the original file without quotes. I wanted one of those strings to be surrounded with single quotes: Changing the default_style parameter in yaml_dump affects whole file, which is not desired. I thought about adding singl…

ntlk: how to get inflections of words

I have a list of words, nearly 5000 English words, and for each word I need these inflectional forms: noun: singular and plural verb: infinitive, present simple, present simple 3rd person, past simple, present participle (ing form), past participle adjective: comparative and superlative adverb How can I extra…

Hide tick label values but keep axis labels

I have this image: I want to hide the numbers; if I use: …I get this image: It also hide the labels, V and t. How can I keep the labels while hiding the values? Answer If you use the matplotlib object-oriented approach, this is a simple task using ax.set_xticklabels() and ax.set_yticklabels(). Here we c…

String/regex search over Excel in Python issue

I’m a newb to SO, and relatively new to Python, so i’m sorry if this is a simple fix or an inappropriate question. Firstly, my program generally works, but i’m trying to implement some redundancy/catchalls for to make it robust. The program looks over a directory (and sub-dirs) of excel file…

pandas dataframe str.contains() AND operation

I have a df (Pandas Dataframe) with three rows: The function df.col_name.str.contains(“apple|banana”) will catch all of the rows: How do I apply AND operator to the str.contains() method, so that it only grabs strings that contain BOTH “apple” & “banana”? I’d like…

how to turn protocol number to name with python?

the protocol like tcp and udp is all represented by a number. the above code will return 6. How I can get the protocol name if I know the protocol number? Answer I’m going to say there is almost definitely a better way then this, but all the protocol names (and values) are stored as constants prefixed b…

Python – Tree without classes

How can I implement a tree without using classes in python? I can only use lists, dictionaries and queue. Obviously without the library bintree. Answer I usually use defaultdict: Usage: bonus: Maria Callas singing O Mio Babbino Caro

Pandas replace all items in a row with NaN if one value is NaN

I want to get rid of some records with NaNs. This works perfectly: However, it changes the shape of my dataframe, and the index is no longer uniformly spaced. Therefore, I’d like to replace all items in these rows with np.nan. Is there a simple way to do this? I was thinking about resampling the datafra…