To select the rows of your dataframe you can use iloc, you can then select the columns you want using square brackets.

For example:

 df = pd.DataFrame(data=[[1,2,3]]*5, index=range(3, 8), columns = ['a','b','c'])

gives the following dataframe:

   a  b  c
3  1  2  3
4  1  2  3
5  1  2  3
6  1  2  3
7  1  2  3

to select only the 3d and fifth row you can do:

df.iloc[[2,4]]

which returns:

   a  b  c
5  1  2  3
7  1  2  3

if you then want to select only columns b and c you use the following command:

df[['b', 'c']].iloc[[2,4]]

which yields:

   b  c
5  2  3
7  2  3

To then get the mean of this subset of your dataframe you can use the df.mean function. If you want the means of the columns you can specify axis=0, if you want the means of the rows you can specify axis=1

thus:

df[['b', 'c']].iloc[[2,4]].mean(axis=0)

returns:

b    2
c    3

As we should expect from the input dataframe.

For your code you can then do:

 df[column_list].iloc[row_index_list].mean(axis=0)

EDIT after comment: New question in comment: I have to store these means in another df/matrix. I have L1, L2, L3, L4...LX lists which tells me the index whose mean I need for columns C[1, 2, 3]. For ex: L1 = [0, 2, 3] , means I need mean of rows 0,2,3 and store it in 1st row of a new df/matrix. Then L2 = [1,4] for which again I will calculate mean and store it in 2nd row of the new df/matrix. Similarly till LX, I want the new df to have X rows and len(C) columns. Columns for L1..LX will remain same. Could you help me with this?

Answer:

If i understand correctly, the following code should do the trick (Same df as above, as columns I took 'a' and 'b':

first you loop over all the lists of rows, collection all the means as pd.series, then you concatenate the resulting list of series over axis=1, followed by taking the transpose to get it in the right format.

dfs = list()
for l in L:
    dfs.append(df[['a', 'b']].iloc[l].mean(axis=0))

mean_matrix = pd.concat(dfs, axis=1).T
Answer from PdevG on Stack Overflow
Top answer
1 of 2
23

To select the rows of your dataframe you can use iloc, you can then select the columns you want using square brackets.

For example:

 df = pd.DataFrame(data=[[1,2,3]]*5, index=range(3, 8), columns = ['a','b','c'])

gives the following dataframe:

   a  b  c
3  1  2  3
4  1  2  3
5  1  2  3
6  1  2  3
7  1  2  3

to select only the 3d and fifth row you can do:

df.iloc[[2,4]]

which returns:

   a  b  c
5  1  2  3
7  1  2  3

if you then want to select only columns b and c you use the following command:

df[['b', 'c']].iloc[[2,4]]

which yields:

   b  c
5  2  3
7  2  3

To then get the mean of this subset of your dataframe you can use the df.mean function. If you want the means of the columns you can specify axis=0, if you want the means of the rows you can specify axis=1

thus:

df[['b', 'c']].iloc[[2,4]].mean(axis=0)

returns:

b    2
c    3

As we should expect from the input dataframe.

For your code you can then do:

 df[column_list].iloc[row_index_list].mean(axis=0)

EDIT after comment: New question in comment: I have to store these means in another df/matrix. I have L1, L2, L3, L4...LX lists which tells me the index whose mean I need for columns C[1, 2, 3]. For ex: L1 = [0, 2, 3] , means I need mean of rows 0,2,3 and store it in 1st row of a new df/matrix. Then L2 = [1,4] for which again I will calculate mean and store it in 2nd row of the new df/matrix. Similarly till LX, I want the new df to have X rows and len(C) columns. Columns for L1..LX will remain same. Could you help me with this?

Answer:

If i understand correctly, the following code should do the trick (Same df as above, as columns I took 'a' and 'b':

first you loop over all the lists of rows, collection all the means as pd.series, then you concatenate the resulting list of series over axis=1, followed by taking the transpose to get it in the right format.

dfs = list()
for l in L:
    dfs.append(df[['a', 'b']].iloc[l].mean(axis=0))

mean_matrix = pd.concat(dfs, axis=1).T
2 of 2
8

You can select specific columns from a DataFrame by passing a list of indices to .iloc, for example:

df.iloc[:, [2,5,6,7,8]]

Will return a DataFrame containing those numbered columns (note: This uses 0-based indexing, so 2 refers to the 3rd column.)

To take a mean down of that column, you could use:

# Mean along 0 (vertical) axis: return mean for specified columns, calculated across all rows
df.iloc[:, [2,5,6,7,8]].mean(axis=0) 

To take a mean across that column, you could use:

# Mean along 1 (horizontal) axis: return mean for each row, calculated across specified columns
df.iloc[:, [2,5,6,7,8]].mean(axis=1)

You can also supply specific indices for both axes to return a subset of the table:

df.iloc[[1,2,3,4], [2,5,6,7,8]]

For your specific example, you would do:

import pandas as pd
import numpy as np

df = pd.DataFrame( 
np.array([[1,2,3,0,5],[1,2,3,4,5],[1,1,1,6,1],[1,0,0,0,0]]),
columns=["a","b","c","d","q"],
index = [0,1,2,3]
)

#I want mean of 0, 2, 3 rows for each a, b, d columns
#. a b d
#0 1 1 2

df.iloc[ [0,2,3], [0,1,3] ].mean(axis=0)

Which outputs:

a    1.0
b    1.0
d    2.0
dtype: float64

Alternatively, to access via column names, first select on those:

df[ ['a','b','d'] ].iloc[ [0,1,3] ].mean(axis=0)

To answer the second part of your question (from the comments) you can join multiple DataFrames together using pd.concat. It is faster to accumulate the frames in a list and then pass to pd.concat in one go, e.g.

dfs = []
for ix in idxs:
    dfm = df.iloc[ [0,2,3], ix ].mean(axis=0)
    dfs.append(dfm)

dfm_summary = pd.concat(dfs, axis=1) # Stack horizontally
🌐
stataiml
stataiml.com › posts › calculate_mean_sel_columns_python
Calculate Mean of Rows on Selected Columns in pandas DataFrame - stataiml
April 5, 2024 - In pandas DataFrame, you can use the mean() function as shown below to calculate the mean of row values for selected columns.
🌐
Statology
statology.org › home › how to calculate the average of selected columns in pandas
How to Calculate the Average of Selected Columns in Pandas
November 29, 2021 - #define new column that shows the average row value for all columns df['average_all'] = df.mean(axis=1) #view updated DataFrame df points assists rebounds average_all 0 14 5 11 10.000000 1 19 7 8 11.333333 2 9 7 10 8.666667 3 21 9 6 12.000000 4 25 12 6 14.333333 5 29 9 5 14.333333 6 20 9 9 12.666667 7 11 4 12 9.000000
🌐
Codegive
codegive.com › blog › pandas_row_mean_of_selected_columns.php
Mastering Pandas Row Mean of Selected Columns: Unlock Hidden Data Insights Today!
In pandas, this is achieved by ... When you apply .mean(axis=1) to a selection of columns, pandas iterates through each row within that selection and computes the mean of the values present in those specific columns for that particular row....
🌐
DataScience Made Simple
datasciencemadesimple.com › home › mean function in python pandas (dataframe, row and column wise mean)
Mean Function in Python pandas (Dataframe, Row and column wise mean) - DataScience Made Simple
December 24, 2020 - mean() – Mean Function in python pandas is used to calculate the arithmetic mean of a given set of numbers, mean of a data frame ,column wise mean or mean of column in pandas and row wise mean or mean of rows in pandas , lets see an example ...
🌐
Iditect
iditect.com › faq › python › calculate-mean-for-selected-rows-for-selected-columns-in-pandas-data-frame.html
Calculate mean for selected rows for selected columns in pandas data frame
To calculate the mean for selected rows and selected columns in a Pandas DataFrame, you can use the loc indexer to specify the rows and columns you want to include in the calculation.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.mean.html
pandas.DataFrame.mean — pandas 3.0.6 documentation
Return the mean of the values over the requested axis. ... Axis for the function to be applied on. For Series this parameter is unused and defaults to 0. For DataFrames, specifying axis=None will apply the aggregation across both axes. Added in version 2.0.0. ... Exclude NA/null values when computing the result. ... Include only float, int, boolean columns...
Find elsewhere
🌐
CSDN
devpress.csdn.net › python › 62fda99e7e66823466192c87.html
Calculate mean for selected rows for selected columns in pandas data frame_python_Mangs-Python
August 18, 2022 - If you want the means of the columns you can specify axis=0, if you want the means of the rows you can specify axis=1 ... As we should expect from the input dataframe. ... EDIT after comment: New question in comment: I have to store these means in another df/matrix.
🌐
W3Schools
w3schools.com › python › pandas › ref_df_mean.asp
Pandas DataFrame mean() Method
Mean, Median, and Mode: Mean - The average value · Median - The mid point value · Mode - The most common value · By specifying the column axis (axis='columns'), the mean() method searches column-wise and returns the mean value for each row.
🌐
datagy
datagy.io › home › pandas tutorials › data analysis in pandas › pandas mean: calculate pandas average for one or multiple columns
Pandas Mean: Calculate the Pandas Average • datagy
December 15, 2022 - Now, alternatively, you could return the mean for everyone row. You can do this by not including the row selection and modifying the axis= parameter. ... If you wanted to calculate the average of multiple columns, you can simply pass in the .mean() method to multiple columns being selected.
🌐
Python Examples
pythonexamples.org › pandas-dataframe-mean
Pandas DataFrame.mean: Compute the Mean of DataFrame Values
import pandas as pd # Create a DataFrame df = pd.DataFrame({ 'A': [10, 20, 30, 40], 'B': [5, 15, 25, 35], 'C': [2, 4, 6, 8] }) # Compute mean for each column column_mean = df.mean() print(column_mean) ... Setting axis=1 computes the mean for each row.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-calculate-row-mean-in-python-dataframes
5 Best Ways to Calculate Row Mean in Python DataFrames – Be on the Right Side of Change
February 19, 2024 - To calculate the row means of the scores, we first select only the ‘Math’ and ‘Science’ columns and then apply the mean() function with axis=1 to the resulting DataFrame. For those already utilizing numpy, or when working with larger, multi-dimensional data, you can convert the DataFrame ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-pandas-dataframe-mean
Pandas DataFrame mean() Method - GeeksforGeeks
May 17, 2024 - By default, describe() works with ... data, offering tailore ... A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. We can perform basic operations on rows/columns like selecting, deleting, adding, and renaming. In this article, we are using nba.csv file. Dealing with Columns In order to deal with col ... Pandas provide a ...
🌐
Easy Tweaks
easytweaks.com › pandas-mean-column-dataframe
Calculate mean of one or more columns in Pandas ...
July 19, 2021 - Master meetings, chats, channels and online collaboration · Go beyond the basics in Word, Excel, PowerPoint and Outlook
🌐
IncludeHelp
includehelp.com › python › compute-row-average-in-pandas.aspx
Compute row average in pandas
July 28, 2022 - We use pandas.DataFrame.mean(axis=0) directly to calculate the average value of row. The average of a particular set of values is the sum of all the values divided by the total number of values. ... # Importing Pandas package import pandas as pd # Creating a Dictionary d = { 'Physics': ['78', ...
🌐
Vultr Docs
docs.vultr.com › python › third party › pandas › dataframe › mean()
Python Pandas DataFrame mean() - Calculate Column Mean
December 24, 2024 - Applying mean() computes the average across each numeric column, resulting in a Series where each index corresponds to a column name from the DataFrame. Understand that the mean() function can compute along different axes.