Convert a namedtuple into a dictionary

I have a named tuple class in python class Town(collections.namedtuple(‘Town’, [ ‘name’, ‘population’, ‘coordinates’, ‘population’, ‘capital’, ‘state_bird’])): # … I’d like to convert Town instances into dictionaries. I don’t want it to be rigidly tied to the names or number of the fields in a Town. Is there a way to write it such that … Read more

How to form tuple column from two columns in Pandas

I’ve got a Pandas DataFrame and I want to combine the ‘lat’ and ‘long’ columns to form a tuple. <class ‘pandas.core.frame.DataFrame’> Int64Index: 205482 entries, 0 to 209018 Data columns: Month 205482 non-null values Reported by 205482 non-null values Falls within 205482 non-null values Easting 205482 non-null values Northing 205482 non-null values Location 205482 non-null values … Read more

Unpacking a list / tuple of pairs into two lists / tuples

Possible Duplicate: A Transpose/Unzip Function in Python I have a list that looks like this: list = ((‘1′,’a’),(‘2′,’b’),(‘3′,’c’),(‘4′,’d’)) I want to separate the list in 2 lists. list1 = (‘1′,’2′,’3′,’4’) list2 = (‘a’,’b’,’c’,’d’) I can do it for example with: list1 = [] list2 = [] for i in list: list1.append(i[0]) list2.append(i[1]) But I want … Read more