Python Forum
calculate specified column or row mean - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: calculate specified column or row mean (/thread-3402.html)



calculate specified column or row mean - SriRajesh - May-20-2017

Hi,

I have the data pandas.DataFrame as below:
I want to compute column 3 mean and row 4 mean.
test:

AAB   BT     2     5  5.1
0  YUT  HYT  89.0  52.0  3.0
4  YHT   JU  25.0  63.0  2.0
0  YUT  HYT  89.0  52.0  3.0
4  YHT   JU  25.0  63.0  2.0
0  YUT  HYT  89.0  52.0  3.0
4  YHT   JU  25.0  63.0  2.0

I use the below command, but it give error:
DataFrame.mean(test[:,2])



RE: calculate specified column or row mean - sparkz_alot - May-20-2017

In order to get an answer relevant to your problem, post the actual section of code you think is the problem as well as the entire error code.  Otherwise you'll get answers like: total all the elements in column 3 and divide the total by the number of elements in column 3, do the same for column 4.


RE: calculate specified column or row mean - SriRajesh - May-20-2017

My code below:
i
mport glob
import pandas as pd
path =r'D:\Mekala_Backupdata\PythonCodes\InputFiles' # use your path
allFiles = glob.glob(path + "/*.csv")
frame = pd.DataFrame()
list_ = []
for file_ in allFiles:
    df = pd.read_csv(file_,index_col=None, header=0)
#    df = pd.read_csv(file_,index_col=None)
    list_.append(df)
frame = pd.concat(list_)
test=frame.dropna()
my data is stored in test.

My output show below:

Output:
test Out[53]:     AAB   BT     2     5  5.1 0  YUT  HYT  89.0  52.0  3.0 4  YHT   JU  25.0  63.0  2.0 0  YUT  HYT  89.0  52.0  3.0 4  YHT   JU  25.0  63.0  2.0 0  YUT  HYT  89.0  52.0  3.0 4  YHT   JU  25.0  63.0  2.0
but I want to calculate 3rd row mean and 4th row mean.
I tried as below: but it give error.

test.mean([0,3])

Error:
Traceback (most recent call last):   File "<ipython-input-54-e915aed94ad8>", line 1, in <module>     test.mean([0,3])   File "C:\Users\user\Anaconda3\lib\site-packages\pandas\core\generic.py", line 5632, in stat_func     numeric_only=numeric_only)   File "C:\Users\user\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4930, in _reduce     axis = self._get_axis_number(axis)   File "C:\Users\user\Anaconda3\lib\site-packages\pandas\core\generic.py", line 328, in _get_axis_number     axis = self._AXIS_ALIASES.get(axis, axis) TypeError: unhashable type: 'list'