Difference between groupby and pivot_table for pandas dataframes

Both pivot_table and groupby are used to aggregate your dataframe. The difference is only with regard to the shape of the result. Using pd.pivot_table(df, index=[“a”], columns=[“b”], values=[“c”], aggfunc=np.sum) a table is created where a is on the row axis, b is on the column axis, and the values are the sum of c. Example: df … Read more

Good alternative to Pandas .append() method, now that it is being deprecated?

Create a list with your dictionaries, if they are needed, and then create a new dataframe with df = pd.DataFrame.from_records(your_list). List’s “append” method are very efficient and won’t be ever deprecated. Dataframes on the other hand, frequently have to be recreated and all data copied over on appends, due to their design – that is … Read more

How to merge a Series and DataFrame

Update From v0.24.0 onwards, you can merge on DataFrame and Series as long as the Series is named. df.merge(s.rename(‘new’), left_index=True, right_index=True) # If series is already named, # df.merge(s, left_index=True, right_index=True) Nowadays, you can simply convert the Series to a DataFrame with to_frame(). So (if joining on index): df.merge(s.to_frame(), left_index=True, right_index=True)

tech