簡體   English   中英

如何格式化/重塑我的數據以獲得每個 object 的單獨數據?

[英]How do I format/reshape my data to get separate data for each object?

目前,我的數據在一個嵌套列表中,如 [[ID1, Pos1, Time1], [ID2, Pos2, Time2], [ID1, Pos3, Time3].....]。

然后我將其放入 Pandas dataframe。我想分別在 883276440288 中針對每個 object ID 繪制 position(Pos 值)與時間的關系圖,我對 plotly 文檔的搜索沒有幫助。 我想要的是每個 object ID 在同一個數字上有一個單獨的位置與時間線 plot。

我該怎么做? 我不確定我是否應該首先重塑我的數據以便以某種方式對重復的 ID 值進行分組,或者我是否可以直接從 plotly 繪圖 function 中執行此操作。我是新手,所以任何幫助將不勝感激。

這是代碼片段:

dataset=[]
for obj in scene.objects:
    obj_data = [obj.ID, obj.time, obj.pos]
    dataset.append(obj_data)

df = pd.DataFrame(data=dataset, columns=["ID", "Time", "Pos"])
 

此外,dataframe 的前五個條目中的 output:

    ID  Time  ...  Speed  
0   4   0.0  ...   1.465062   
1   5   0.0  ...   1.195697 
2   6   0.0  ...   1.443732  
3   7   0.0  ...   1.318886   
4   8   0.0  ...   1.258748  

您可以按“ID”對 dataframe 中的數據進行分組,然后在同一圖中添加一行 plot:

import plotly.graph_objs as go


fig = go.Figure()  # Same figure object

for group_name, group_df in df.groupby(["ID"]):
    # group_name is the "ID" value
    # group_df is a dataframe with the rows of each "ID" value
    fig.add_trace(
        go.Scatter(  # This is how we draw a line plot
            x=group_df["Time"], y=group_df["Pos"],
            name=group_name,  # Add the "ID" as the name of the line
        )
    )

fig.show()  # Display figure

這是一個樣本輸入,預期 output 使用此代碼:

   ID  Pos  Time
0   1    1     1
1   1    3     2
2   0    2     1
3   0    4     2

使用 Plotly 創建的繪圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM