Posts

Showing posts with the label Append

Adding Dictionaries Together, Python

Answer : If you're interested in creating a new dict without using intermediary storage: (this is faster, and in my opinion, cleaner than using dict.items()) dic2 = dict(dic0, **dic1) Or if you're happy to use one of the existing dicts: dic0.update(dic1) Here are quite a few ways to add dictionaries. You can use Python3's dictionary unpacking feature. ndic = {**dic0, **dic1} Or create a new dict by adding both items. ndic = dict(dic0.items() + dic1.items()) If your ok to modify dic0 dic0.update(dic1) If your NOT ok to modify dic0 ndic = dic0.copy() ndic.update(dic1) If all the keys in one dict are ensured to be strings ( dic1 in this case, of course args can be swapped) ndic = dict(dic0, **dic1) In some cases it may be handy to use dict comprehensions (Python 2.7 or newer), Especially if you want to filter out or transform some keys/values at the same time. ndic = {k: v for d in (dic0, dic1) for k, v in d.items()} >>> dic0 = {...

Concatenate Pandas DataFrames Generated With A Loop

Answer : Pandas concat takes a list of dataframes. If you can generate a list of dataframes with your looping function, once you are finished you can concatenate the list together: data_day_list = [] for i, day in enumerate(list_day): data_day = df[df.day==day] data_day_list.append(data_day) final_data_day = pd.concat(data_day_list) Exhausting a generator is more elegant (if not more efficient) than appending to a list. For example: def yielder(df, list_day): for i, day in enumerate(list_day): yield df[df['day'] == day] final_data_day = pd.concat(list(yielder(df, list_day)) Appending or concatenating pd.DataFrame s is slow. You can use a list in the interim and then create the final pd.DataFrame at the end with pd.DataFrame.from_records() e.g.: interim_list = [] for i,(k,g) in enumerate(df.groupby(['[*name of your date column here*'])): if i % 1000 == 0 and i != 0: print('iteration: {}'.format(i)) # just tells you where you are i...