-1

When I run the following code

import numpy as np

A = np.random.rand(10)
A.groupby

I get the error AttributeError: 'numpy.ndarray' object has no attribute 'groupby'.

I uninstalled numpy, by running pip3 uninstall numpy multiple times, until it said WARNING: Skipping numpy as it is not installed.. I upgraded pip, and I reinstalled numpy.

How do I fix this error?

I am using pip 19.3.1 and numpy 1.17.4.

usernumber
  • 1,958
  • 1
  • 21
  • 58
  • May be this will help : https://stackoverflow.com/questions/38013778/is-there-any-numpy-group-by-function – Prince Francis Dec 16 '19 at 11:11
  • what is your expected output? – Dishin H Goyani Dec 16 '19 at 11:12
  • 2
    How come you think `groupby` is a method of numpy arrays? Maybe you look for `pandas.DataFrame(np.random.rand(10)).groupby()`? – Alfe Dec 16 '19 at 11:13
  • @PrinceFrancis Not really. The answer to that question is the function I'm trying to use and that raises an error. – usernumber Dec 16 '19 at 11:14
  • @Alfe I was trying to use the code from this answer https://stackoverflow.com/a/48602194/3128109 where `groupby` is used as an `numpy.ndarray` method – usernumber Dec 16 '19 at 11:15
  • 1
    That's using a dataframe. Numpy doesn't have a `groupby` so the only fix is to not try and use a method that doesn't exist – roganjosh Dec 16 '19 at 11:19
  • 3
    In your comment in the question you linked to you made the error of passing a numpy-array instead of a pandas-dataframe. Stick to the provided code (`data = pd.DataFrame({ ...})` and `train_test_eq_split(data, ...)`), and it should work. – Alfe Dec 16 '19 at 11:19
  • 1
    Oh, I get it. Thanks! – usernumber Dec 16 '19 at 11:20

1 Answers1

1

The error you got tells you exactly what the problem is: numpy arrays do not have the attributegroupby. I figure you wanted to use the method groupby(), but numpy doesn't have that either. What you want to do is use a pandas dataframe and then group by a specific column, like this

df = pd.DataFrame({'Animal': ['Falcon', 'Falcon', 'Parrot', 'Parrot'], 'Max Speed': [380., 370., 24., 26.]})
df.groupby(['Animal'])

Example taken from here

emilaz
  • 1,722
  • 1
  • 15
  • 31