简体   繁体   English

如何格式化/重塑我的数据以获得每个 object 的单独数据?

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

Currently, my data is in a nested list as [[ID1, Pos1, Time1], [ID2, Pos2, Time2], [ID1, Pos3, Time3]....].目前,我的数据在一个嵌套列表中,如 [[ID1, Pos1, Time1], [ID2, Pos2, Time2], [ID1, Pos3, Time3].....]。

I then put that into a Pandas dataframe. I want to graph position (Pos values) against time for each object ID separately in Plotly, and my searches of plotly documentation haven't helped.然后我将其放入 Pandas dataframe。我想分别在 883276440288 中针对每个 object ID 绘制 position(Pos 值)与时间的关系图,我对 plotly 文档的搜索没有帮助。 What I'd like is for each object ID to have a separate Pos vs Time line plot on the same figure.我想要的是每个 object ID 在同一个数字上有一个单独的位置与时间线 plot。

How would I do this?我该怎么做? I'm unsure whether I should reshape my data first so that repeated ID values are grouped somehow or whether I can do this direct from the plotly plotting function. I'm new at this, so any help would be greatly appreciated.我不确定我是否应该首先重塑我的数据以便以某种方式对重复的 ID 值进行分组,或者我是否可以直接从 plotly 绘图 function 中执行此操作。我是新手,所以任何帮助将不胜感激。

Here's the code snippet:这是代码片段:

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"])
 

Also, the output of the first five entries of the dataframe:此外,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  

You could group the data in your dataframe by "ID" and then add a line plot to the same figure:您可以按“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

This is a sample input and expected output using this code:这是一个样本输入,预期 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