简体   繁体   English

如何在没有重复的情况下更新da Pandas Panel

[英]How to update da Pandas Panel without duplicates

Currently i'm working on a Livetiming-Software for a motorsport-application. 目前,我正在为赛车应用程序开发Livetiming软件。 Therefore i have to crawl a Livetiming-Webpage and copy the Data to a big Dataframe. 因此,我必须抓取Livetiming网页并将数据复制到大数据框。 This Dataframe is the source of several diagramms i want to make. 该数据框是我要制作的几个图表的来源。 To keep my Dataframe up to date, i have to crawl the webpage very often. 为了使我的数据框保持最新状态,我不得不经常抓取该网页。

I can download the Data and save them as a Panda.Dataframe. 我可以下载数据并将其另存为Panda.Dataframe。 But my Problem is step from the downloaded DataFrame to the Big Dataframe, that includes all the Data. 但是我的问题是从下载的DataFrame到包含所有数据的Big Dataframe。

import pandas as pd
import numpy as np
df1= pd.DataFrame({'Pos':[1,2,3,4,5,6],'CLS':['V5','V5','V5','V4','V4','V4'],
                 'Nr.':['13','700','30','55','24','985'],
                 'Zeit':['1:30,000','1:45,000','1:50,000','1:25,333','1:13,366','1:17,000'],
                 'Laps':['1','1','1','1','1','1']})

df2= pd.DataFrame({'Pos':[1,2,3,4,5,6],'CLS':['V5','V5','V5','V4','V4','V4'],
                 'Nr.':['13','700','30','55','24','985'],
                 'Zeit':[np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,],
                 'Laps':['2','2','2','2','2','2']})
df3= pd.DataFrame({'Pos':[1,2,3,4,5,6],'CLS':['V5','V5','V5','V4','V4','V4'],
                 'Nr.':['13','700','30','55','24','985'],
                 'Zeit':['1:31,000','1:41,000','1:51,000','1:21,333','1:11,366','1:11,000'],
                 'Laps':['2','2','2','2','2','2']})
df1.set_index(['CLS','Nr.','Laps'],inplace=True)
df2.set_index(['CLS','Nr.','Laps'],inplace=True)
df3.set_index(['CLS','Nr.','Laps'],inplace=True)

df1 shows a Dataframe from previous laps. df1显示了前几圈的数据帧。 df2 shows a Dataframe in the second lap. df2在第二圈显示了一个数据框。 The Lap is not completed, so i have a nan. 膝部未完成,所以我有男。 df3 shows a Dataframe after the second lap is completed. df3显示第二圈完成后的数据帧。

My target is to have just one row for each Lap per Car per Class. 我的目标是每班每辆汽车的每一圈只有一行。 Either i have the problem, that i have duplicates with incomplete Laps or all date get overwritten. 我有一个问题,就是我的重复项不完整或所有日期都被覆盖。

I hope that someone can help me with this problem. 我希望有人可以帮助我解决这个问题。

Thank you so far. 到目前为止谢谢你。

MrCrunsh 克鲁什先生

If I understand your problem correctly, your issue is that you have overlapping data for the second lap: information while the lap is still in progress and information after it's over. 如果我正确地理解了您的问题,那么您的问题是第二圈的数据重叠:该圈仍在进行中的信息和结束后的信息。 If you want to put all the information for a given lap in one row, I'd suggest use multi-index columns or changing the column names to reflect the difference between measurements during and after laps. 如果要将给定单圈的所有信息都放在一行中,我建议使用多索引列或更改列名以反映单圈之间和之后的测量之间的差异。

df = pd.concat([df1, df3])
df = pd.concat([df, df2], axis=1, keys=['after', 'during'])

The result will look like this: 结果将如下所示:

             after           during
               Pos      Zeit    Pos Zeit
CLS Nr. Laps
V4  24  1        5  1:13,366    NaN  NaN
        2        5  1:11,366    5.0  NaN
    55  1        4  1:25,333    NaN  NaN
        2        4  1:21,333    4.0  NaN
    985 1        6  1:17,000    NaN  NaN
        2        6  1:11,000    6.0  NaN
V5  13  1        1  1:30,000    NaN  NaN
        2        1  1:31,000    1.0  NaN
    30  1        3  1:50,000    NaN  NaN
        2        3  1:51,000    3.0  NaN
    700 1        2  1:45,000    NaN  NaN
        2        2  1:41,000    2.0  NaN

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

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