在pandas中循环搜索数据帧并更新列表卡住

问题描述 投票:0回答:1

我想在我的数据帧列'which_AOI'中计算感兴趣的区域(范围从0到9)。我希望有一个新的列,结果根据变量'marker'(范围从0到x)添加到数据帧中,它告诉我何时完成一个'图片'并且下一个'图片'开始(一个标记可以继续可变长度的行)。这是我的代码到目前为止,但似乎卡住并运行而不提供输出。我尝试从头开始重建它,但是当我到达'if df.marker == num'时它就不会停止。我错过了什么? (下面的示例数据框)

## AOI count of spec. type function (in progress):
import numpy as np
import pandas as pd

path_i = "/Users/Desktop/Pilot/results/gazedata_filename.csv"
df = pd.read_csv(path_i, sep =",")

#create a new dataframe for AOIs:

d = {'marker': []}
df_aoi = pd.DataFrame(data=d)

### Creating an Aoi list
item = df.which_AOI
aoi = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  #list for search
aoi_array = [0, 0 , 0, 0, 0, 0, 0, 0, 0, 0] #list for filling
num = 0 

for i in range (0, len (df.marker)): #loop through the dataframe
    if df.marker == num:  ## if marker = num its one picture 
       for index, item in enumerate(aoi): #look for item (being a number in which_AOI) in aoi list
           if (item == aoi[index]):
                aoi_array[index] += 1
        print (aoi)
        print (aoi_array)
        se = pd.Series(aoi_array) # make list into a series to attach to dataframe
        df_aoi['new_col'] = se.values #add list to dataframe 
        aoi_array.clear()  #clears list before next picture
    else:
        num +=1 



index       pos_time        pos_x      pos_y       pup_time   pup_diameter   marker  which_AOI  fixation  Picname   shock
1   16300   168.608779907227    -136.360855102539   16300   2.935715675354      0     7             18      5    save
2   16318   144.97673034668 -157.495513916016   16318   3.08838820457459        0       8           33      5   save
3   16351   152.92560577392598  -156.64172363281298 16351   3.0895299911499     0      7             17     5   save
4   16368   152.132453918457    -157.989685058594   16368   3.111008644104      0     7              18     5   save
5   16386   151.59835815429702  -157.55587768554702 16386   3.09514689445496    0      7              18    5   save
6   16404   150.88092803955098  -152.69479370117202 16404   3.10009074211121    1      7              37    5   save
7   16441   152.76554107666 -142.06188964843798 16441   3.0821495056152304     1       7              33    5   save
list pandas for-loop marker
1个回答
0
投票

根据您的问题不是100%清除,但听起来您想要计算每个标记中每个which_AOI值的行数。

你可以使用groupby来完成这个任务

df_aoi = df.groupby(['marker','which_AOI']).size().unstack('which_AOI',fill_value=0)

在:

   pos_time       pos_x       pos_y  pup_time  pup_diameter  marker  \
0     16300  168.608780 -136.360855     16300      2.935716       0   
1     16318  144.976730 -157.495514     16318      3.088388       0   
2     16351  152.925606 -156.641724     16351      3.089530       0   
3     16368  152.132454 -157.989685     16368      3.111009       0   
4     16386  151.598358 -157.555878     16386      3.095147       0   
5     16404  150.880928 -152.694794     16404      3.100091       1   
6     16441  152.765541 -142.061890     16441      3.082150       1   

   which_AOI  fixation  Picname shock  
0          7        18        5  save  
1          8        33        5  save  
2          7        17        5  save  
3          7        18        5  save  
4          7        18        5  save  
5          7        37        5  save  
6          7        33        5  save  

日期:

which_AOI  7  8
marker         
0          4  1
1          2  0
© www.soinside.com 2019 - 2024. All rights reserved.