If you only want the mean of the weight column, select the column (which is a Series) and call .mean():
In [479]: df
Out[479]:
ID birthyear weight
0 619040 1962 0.123123
1 600161 1963 0.981742
2 25602033 1963 1.312312
3 624870 1987 0.942120
In [480]: df.loc[:, 'weight'].mean()
Out[480]: 0.83982437500000007
Answer from DSM on Stack OverflowIf you only want the mean of the weight column, select the column (which is a Series) and call .mean():
In [479]: df
Out[479]:
ID birthyear weight
0 619040 1962 0.123123
1 600161 1963 0.981742
2 25602033 1963 1.312312
3 624870 1987 0.942120
In [480]: df.loc[:, 'weight'].mean()
Out[480]: 0.83982437500000007
Try df.mean(axis=0) , axis=0 argument calculates the column wise mean of the dataframe so the result will be axis=1 is row wise mean so you are getting multiple values.
Finding mean of column in a pandas dataframe where the values in column may be string.
In a pandas dataframe, how to find the average value within specific categories as denoted in another column. See example in the description.
What’s your favorite way to add a “total” row at the bottom of a dataFrame?
Pandas - create new column with average of other columns in CSV
So , I recently started learning pandas in python and I had an exercise wherein I have to find the average of values in a particular column in a dataframe. Suppose the name of my column is "BasePay", the required command would be:-
dataframe['BasePay'].mean().
However it gives me these errors:-
Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/pandas/core/nanops.py in f(values, axis, skipna, **kwds).
TypeError: unsupported operand type(s) for +: 'float' and 'str'.
Also worth noting that for some values , the "BasePay " column also contains string "Not provided" in place of a numeric value .
So my question is, what adjustments do I need to make in my command to calculate the average of only the numeric values and ignore the strings in the column to find the average of all values in that column?
I have this:
| Category | Value |
|---|---|
| A | 2 |
| A | 4 |
| A | 6 |
| A | 4 |
| B | 3 |
| B | 4 |
| B | 5 |
And I want this:
| Category | Value |
|---|---|
| A | 4 |
| B | 4 |
I want the average values, but specifically within the A and B categories.
Thanks!