Python Forum
calculate specified column or row mean
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calculate specified column or row mean
#1
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])
Reply
#2
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 3,269 May-09-2021, 06:46 PM
Last Post: Pit292

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020