1

Please forgive me if my terminology isn't correct - I'm new to this!

I'm trying to isolate certain entries based on the value of the first element in each item in an array and perform some operations on certain columns. Here's an example of the type of data I'm working with:

[[1, 99, 400],
[1, 95, 200],
[2, 92, 100],
[1, 85, 500],
[2, 88, 300]]

I need to figure out the means of columns 2 and 3 using a for loop, if statement, and arithmetic, respectively, for each condition (reflected in column 1, where values are either 1 or 2).

I'm trying to split the above array into two separate arrays for each condition, and then taking the mean of those columns using numpy.mean. Here's what I want the lists to look like:

cond1 = [[1, 99, 400], [1, 95, 200], [1, 85, 500]]
cond2 = [[2, 92, 100], [2, 88, 300]]

I'm stuck on how to separate these conditions into two new arrays based on the first element. Here's the furthest I've gotten after googling about slicing.. but I'm stuck!

for x in stim:
    if stim_acc[0]==1.0:
        np.where(stim_acc[0] = 1.0)
        cond1 = [[s,a,rt] for s, a, rt in zip(stim, acc, mrt)]
        print(cond1)

regarding stim, acc, and mrt:

I have a several column-long set of data from which I isolated the stimulus (now stim), accuracy (now acc), and mean reaction time (now mrt) entries into a new list (the first in this post). I did this like so:

stim = data[:,1]
 acc = data[:,3]
 mrt = data[:,4]

 stim_acc = [[s, a, rt] for s, a, rt in zip(stim, acc, mrt)]
 print(stim_acc)

I've preemptively named the new list stim_acc because i foresaw this turning into a list calculating the accuracy for each condition over a loop.

Thank you, any help is greatly appreciated.

3
  • Use pandas .groupby() Commented Oct 14, 2019 at 21:54
  • I'm not sure I understand what stim, acc, mrt are meant to be. Can you paste the full code snipet? Commented Oct 14, 2019 at 22:01
  • Just added an explanation for stim, acc, and mrt - let me know if you need more info! Commented Oct 14, 2019 at 22:10

3 Answers 3

1

As i understand, you want to split your list to two list by index[0]'s value. If it is 1 then add to cond1's list, else add to cond2's list. You can implement this using this code:

cond1=[]
cond2=[]
for item in listoflists:
    if item[0] == 1:
        cond1.append(item)
    else 
        cond2.append(item)
Sign up to request clarification or add additional context in comments.

Comments

0

use pandas groupby :

>>> a = [[1, 99, 400],
 [1, 95, 200],
 [2, 92, 100],
 [1, 85, 500],
 [2, 88, 300]]
>>> df = pd.DataFrame(a)
>>> df
   0   1    2
0  1  99  400
1  1  95  200
2  2  92  100
3  1  85  500
4  2  88  300
>>> data = df.groupby([0])
>>> cond = data.groups
>>> df.loc[cond[1]]
   0   1    2
0  1  99  400
1  1  95  200
3  1  85  500
>>> df.loc[cond[2]]
   0   1    2
2  2  92  100
4  2  88  300

Comments

0

Using a simple loop, you can display the means like this:

data=[[1, 99, 400],
[1, 95, 200],
[2, 92, 100],
[1, 85, 500],
[2, 88, 300]]

groups = [], []
for row in data:
    groups[row[0]-1].append(row)

for group in groups:
    print(np.mean(group, axis=0))  # means of each column by group

But it is often better to use a pandas dataframe for this type of task

df = pd.DataFrame(data, columns=["stim", "acc", "mrt"])
for value in df.stim.unique():
    print(df[df.stim == value].mean())

or

for i, group in df.groupby("stim"):
    print(i, group.mean())

(I'm assuming you want the means of each column within each group.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.