To take the average of every two rows in a pandas DataFrame in Python, you can use the groupby()
and mean()
functions. Here's an example:
main.py258 chars12 lines
Output:
main.py36 chars5 lines
In this example, we divide the index of the DataFrame by 2 using the //
operator to generate groups of two rows. Then, we apply the mean()
function to calculate the average within each group. The resulting DataFrame contains the average values for columns 'A' and 'B' for every two rows in the original DataFrame.
gistlibby LogSnag