New answers tagged


There are a few small niceties that you can add. long = pd.DataFrame(columns={'bene_id', 'day','date'}) cols_to_order = ['bene_id', 'day','date'] should reuse the list: cols_to_order = ['bene_id', 'day','date'] long = pd.DataFrame(columns=set(cols_to_order)) This: cols_to_order + (long.columns.drop(cols_to_order).tolist()) can drop the outer parens, since ...


Your rawData (which should be ideally named raw_data, python suggests a style guide to name variables and functions in lower_snake_case) is already in a list structure. You can manipulate this in place, without having to process the whole dataset manually. for row in raw_data: row.update(row.pop("prices"))


Here's code using .apply(), that makes a single pass through the dataframe. df = pd.DataFrame({'category': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 1], 'value': range(12), 'other': list('abcdefghijkl')}) def pick(group, n): """Return n random rows from groups that have at least n rows""" ...


This is not too bad. It's a good thing you use keyword arguments for the replace method I always try to keep my original data in its original state, and continue with the cleaned dataframe. fluent style This lends itself very well to a kind of fluent style as in this example. I use it too, and use a lot of df.assign, df.pipe, df.query... In this example I ...

Top 50 recent answers are included