EDIT: The .mean() function works just right:
renda['age'].mean()
I'm still not sure why the errors mentioned below and in the answers happened, but this helps as a shortcut.
I got a DataFrame renda
in which there's a column age
and I need to get the mean of the ages. I wrote
t_age = renda['age'].sum()
renda.index = renda.index.map(int) #intended solution
last_id = renda.iloc(-1)
print(t_age/last_id)
Trying to adapt the answers from here. It still doesn't work though and this error is raised:
TypeError: unsupported operand type(s) for /: 'int' and '_iLocIndexer'
What can I do to solve this? Thank you!
@ScottBoston:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x, y)
657 result = expressions.evaluate(op, str_rep, x, y,
--> 658 raise_on_error=True, **eval_kwargs)
659 except TypeError:
/opt/conda/lib/python3.6/site-packages/pandas/core/computation/expressions.py in evaluate(op, op_str, a, b, raise_on_error, use_numexpr, **eval_kwargs)
210 return _evaluate(op, op_str, a, b, raise_on_error=raise_on_error,
--> 211 **eval_kwargs)
212 return _evaluate_standard(op, op_str, a, b, raise_on_error=raise_on_error)
/opt/conda/lib/python3.6/site-packages/pandas/core/computation/expressions.py in _evaluate_numexpr(op, op_str, a, b, raise_on_error, truediv, reversed, **eval_kwargs)
121 if result is None:
--> 122 result = _evaluate_standard(op, op_str, a, b, raise_on_error)
123
/opt/conda/lib/python3.6/site-packages/pandas/core/computation/expressions.py in _evaluate_standard(op, op_str, a, b, raise_on_error, **eval_kwargs)
63 with np.errstate(all='ignore'):
---> 64 return op(a, b)
65
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in <lambda>(x, y)
96 default_axis=default_axis, reversed=True),
---> 97 rtruediv=arith_method(lambda x, y: operator.truediv(y, x),
98 names('rtruediv'), op('/'), truediv=True,
TypeError: unsupported operand type(s) for /: 'int' and 'str'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in safe_na_op(lvalues, rvalues)
681 with np.errstate(all='ignore'):
--> 682 return na_op(lvalues, rvalues)
683 except Exception:
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x, y)
667 mask = notnull(x)
--> 668 result[mask] = op(x[mask], y)
669 else:
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in <lambda>(x, y)
96 default_axis=default_axis, reversed=True),
---> 97 rtruediv=arith_method(lambda x, y: operator.truediv(y, x),
98 names('rtruediv'), op('/'), truediv=True,
TypeError: unsupported operand type(s) for /: 'int' and 'str'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-71-d5969a714aff> in <module>()
3 renda.index = renda.index.astype(int) #use astype to convert to int
4 last_id = renda.iloc[-1] #Square brackets
----> 5 print(t_age/last_id)
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in wrapper(left, right, name, na_op)
719 lvalues = lvalues.values
720
--> 721 result = wrap_results(safe_na_op(lvalues, rvalues))
722 return construct_result(
723 left,
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in safe_na_op(lvalues, rvalues)
690 if is_object_dtype(lvalues):
691 return libalgos.arrmap_object(lvalues,
--> 692 lambda x: op(x, rvalues))
693 raise
694
pandas/_libs/algos_common_helper.pxi in pandas._libs.algos.arrmap_object (pandas/_libs/algos.c:31954)()
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in <lambda>(x)
690 if is_object_dtype(lvalues):
691 return libalgos.arrmap_object(lvalues,
--> 692 lambda x: op(x, rvalues))
693 raise
694
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in <lambda>(x, y)
95 rsub=arith_method(lambda x, y: y - x, names('rsub'), op('-'),
96 default_axis=default_axis, reversed=True),
---> 97 rtruediv=arith_method(lambda x, y: operator.truediv(y, x),
98 names('rtruediv'), op('/'), truediv=True,
99 fill_zeros=np.inf, default_axis=default_axis,
TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
@juanpa.arrivillaga:
If position is a string, then you can use int(position) to convert position to an integer. position is an integer.
astype() function to change the data type of index from float to integer type. Output : Example #2: Use Index. astype() function to change the datatype of the given Index to string form.
To change the type of a DataFrame's index in Pandas, use the DataFrame. index. astype(~) method.
To reset the index in pandas, you simply need to chain the function . reset_index() with the dataframe object. On applying the . reset_index() function, the index gets shifted to the dataframe as a separate column.
I think you need this:
t_age = renda['age'].astype(int).sum()
renda.index = renda.index.astype(int) #use astype to convert to int
last_id = renda.iloc[-1] #Square brackets
print(t_age/last_id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With