Pandas (page 5 of 9) |
We can use different conditions to filter our data contained in a DataFrame. For instance, my_df[my_df[year] > 2022] would display those rows in the DataFrame where the value in the year Series is greater than 2022. We can use & (and) or | (or) to add different conditions to our filtering condition.
We can sort values in a column (Series) in ascending order using my_df.sort_values(col1) or in descending order using my_df.sort_values(col2, ascending=False). We can also sort our data by mutliple columns by using my_df.sort_values([col1, col2], ascending=[True, False]).
We can group our data by using a groupby command. For instance, my_df.groupby(col) returns a groupby object for values from one column while my_df.groupby([col1, col2]) returns a groupby object for values from multiple columns. We can then perform calculations on each group (grouped['revenue'].agg([np.size, np.sum, np.mean, np.std])).