简体   繁体   English

pd.DataFrame 正在覆盖列,而不是将数据保存到 pd.DataFrame

[英]pd.DataFrame is overwriting columns, instead of saving data to pd.DataFrame

Here, I am attempting to create a dataframe to compare the location of an object between frames:在这里,我试图创建一个 dataframe 来比较帧之间 object 的位置:

Pcount = []
Pcountdb = []
framenumber = 0
frames_count = 0
frames_count = self.vdo.get(cv2.CAP_PROP_FRAME_COUNT)
df = pd.DataFrame(index=range(int(frames_count))) 
if len(outputs) > 0:
    for i in range(len(outputs):
        bbox_xyxy = outputs[:,:4]
        identities = outputs[:,-1]
        sx = outputs[:,0]
        sy = outputs[:,1]
        ex = outputs[:,2]
        ey = outputs[:,3]
        cx = ((sx + ex) /2)
        cy = ((sy + ey) /2)
        ct = (cx, cy)
        cx2 = (cx.tolist())
        cy2 = (cy.tolist())
        P = identities[i]
        df[str(P.astype(int))] = ""                              
        #creates new column with an id number obtained through deepsort

        df.at[int(framenumber), str(P.astype(int))] = [cx2[i], cy2[i]]  
        #the i function from a for loop is necessary for multiple objects in the same frame

        print(df)

        if not P in Pcountdb:
            global PcountT
            Pcountdb.append(P)
            PcountT = PcountT + 1

framenumber = framenumber + 1

Edited: The script above starts with placeholders编辑:上面的脚本以占位符开头

df = pd.DataFrame... creates my dataframe with a row for each image/frame in my video df = pd.DataFrame... 创建我的 dataframe ,视频中的每个图像/帧都有一行

bbox_xyxy is created once my object detector has been looped over by deepsort, and deepsort has identified each detected object and identified it as an object with a location. bbox_xyxy 是在我的 object 检测器被 deepsort 循环后创建的,并且 deepsort 已识别每个检测到的 object 并将其识别为具有位置的 object。

I then, break apart the np.arrays and calculate the center points of these objects so they can be seen as an individual point, instead of a bounding box rectangle.然后,我分解 np.arrays 并计算这些对象的中心点,以便可以将它们视为单个点,而不是边界框矩形。

Pandas takes my inputs and creates a DataFrame with the object id (in this case, 1), the center xy coordinates and places them in the row corresponding with each frame Pandas 接受我的输入并创建一个 DataFrame 与 object id(在本例中为 1),中心 xy 坐标并将它们放置在与每个帧对应的行中

next, we print the dataframe and view the results接下来,我们打印 dataframe 并查看结果

print(df) returns: print(df) 返回:

                     1
Frames                
3       [614.5, 632.0]

                     1
Frames                
3                     
4       [610.5, 624.0]

                     1
Frames                
3                     
4                     
5       [603.0, 618.0]

                     1
Frames                
3                     
4                     
5                     
6       [574.0, 615.5]

                     1
Frames                
3                     
4                     
5                     
6                     
7       [564.0, 610.0]

                     1
Frames                
3                     
4                     
5                     
6                     
7                     
8       [559.0, 597.0]

The DataFrame keeps track of ONLY the most recent set of coordinates per column. DataFrame 仅跟踪每列的最新坐标集。 If I were to produce two columns, only the last sighting of each object will be present in my dataframe (as shown above with one object, identified as 1)如果我要生成两列,那么我的 dataframe 中只会出现每个 object 的最后一次出现(如上图所示,其中一个 object,标识为 1)

I need to save the output to my pd.DataFrame = df, instead of being overwritten.我需要将 output 保存到我的 pd.DataFrame = df,而不是被覆盖。

                     1
Frames                
3       [614.5, 632.0]
4       [610.5, 624.0]
5       [603.0, 618.0]
6       [574.0, 615.5]
7       [564.0, 610.0]
8       [559.0, 597.0]

So I can compare locations of these objects between frames, giving me an object count that counts objects and stores them in 2 databases, "UP and "DOWN"所以我可以比较这些对象在帧之间的位置,给我一个 object 计数,对对象进行计数并将它们存储在 2 个数据库中,“UP”和“DOWN”

Your DataFrame is just adding last raw because every time for loop runs you're resetting column to null.您的 DataFrame 只是添加最后一个原始数据,因为每次 for 循环运行时,您都会将列重置为 null。 so all previous values are erased.所以所有以前的值都被删除了。 by looking at your code I can see that as your code doesn't require to be in for loop.通过查看您的代码,我可以看到,因为您的代码不需要处于 for 循环中。

solution :解决方案

Pcount = []
Pcountdb = []
framenumber = 0
frames_count = 0
frames_count = self.vdo.get(cv2.CAP_PROP_FRAME_COUNT)
df = pd.DataFrame(index=range(int(frames_count))) 
if len(outputs) > 0:
    bbox_xyxy = outputs[:,:4]
    identities = outputs[:,-1]
    sx = outputs[:,0]
    sy = outputs[:,1]
    ex = outputs[:,2]
    ey = outputs[:,3]
    cx = ((sx + ex) /2)
    cy = ((sy + ey) /2)
    ct = (cx, cy)
    cx2 = (cx.tolist())
    cy2 = (cy.tolist())
    P = identities[i]
    df[str(P.astype(int))] = ""
    for i in range(len(outputs):
        df.at[int(framenumber), str(P.astype(int))] = [cx2[i], cy2[i]]
        print(df)

Hope this works.希望这有效。

for i in range(len(outputs)):
                    P = identities[i]

                    if not P in Pcountdb:
                        df[str(P.astype(int))] = ""
                        global PcountT
                        Pcountdb.append(P)
                        PcountT = PcountT + 1
                    else:
                        if P in Pcountdb:
                            df.at[int(framenumber), str(P.astype(int))] = [cx2[i], cy2[i]]


[222 rows x 1 columns]
                     1
 Frames                
 4       [610.5, 624.0]
 5       [603.0, 618.0]
 6       [574.0, 615.5]
 7       [564.0, 610.0]
 8       [559.0, 597.0]
 ...                ...
 226     [640.5, 518.5]
 227     [643.0, 525.0]
 228     [646.0, 529.5]
 229     [647.5, 529.5]
 230     [650.5, 531.5]

Thank you @Adarsh for your response, you were right, my columns were being overwritten because i was creating them from a loop.谢谢@Adarsh 的回复,你是对的,我的列被覆盖了,因为我是从循环中创建它们的。

I took the line df[str(P.astype(int))] = "", where columns are created and ran it with an exclusive circumstance.我采用了 df[str(P.astype(int))] = "" 行,其中创建了列并在排他的情况下运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM