[Tutor] Extract several arrays from a large 2D array
Peter Otten
__peter__ at web.de
Thu Jan 21 05:49:15 EST 2016
Ek Esawi wrote:
> Hi All--
> I have a 2D array (2000, 4); an example is given abelow. On the 1st
> column I have 15 variables, on the 2nd 4 variables. Ignore column 3 for
> now. I want a code that generate 4 arrays for each variable on the 1st
> column; each array contains all the values from column 4. Then I want to
> find the average of each array.
> For example; pick the 1st variable in column 1 which is 1; then pick the
> 1st variable on the 2nd column which is 5. Now I want an array that
> contains all the values on the 4th column that match variable 1 on the 1st
> and variable 5 on the 2nd column. I need to get the average of each array.
>
>
> A b c d
>
> 1 5 3 4
>
> 1 3 2 7
>
> 2 5 7 5
>
> 3 2 8 5
>
> 2 3 2 3
With pandas:
>>> df = pandas.DataFrame([
... [1, 5, 3, 4],
... [1, 5, 2, 2],
... [2, 5, 1, 2],
... [2, 3, 1, 2]],
... columns=["a", "b", "c", "d"])
>>> df.groupby(("a", "b"))["d"].mean()
a b
1 5 3
2 3 2
5 2
Name: d, dtype: int64
More information about the Tutor
mailing list