Plotly 图表的数据帧格式 - Pandas python

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

我得到了一个包含一些列的数据框,我需要使用该数据来绘制折线图。

奥瑞金 时间戳日期
橙色 2023-02-03 00:00:00
橙色 2023-02-03 00:00:00
橙色 2023-02-10 00:00:00
苹果 2023-02-24 00:00:00
苹果 2023-04-18 00:00:00
苹果 2023-04-18 00:00:00

我需要将其转换为这种形式。

奥瑞金 时间戳日期 计数
橙色 2023-02-03 00:00:00 2
橙色 2023-02-10 00:00:00 1
苹果 2023-02-24 00:00:00 1
苹果 2023-04-18 00:00:00 2

我尝试使用 PD.MELT 和 groupby 但没有成功。 我正在尝试复制这样的东西:

Plotly Graph that i need to replicate with my data.

谢谢!

python pandas dataframe plotly plotly-python
1个回答
0
投票
data = {
    "ORIGEN": ["ORANGE", "ORANGE", "ORANGE", "APPLE", "APPLE", "APPLE"],
    "TIMESTAMP DATE": ["2023-02-03 00:00:00", "2023-02-03 00:00:00", "2023-02-10 00:00:00",
                       "2023-02-24 00:00:00", "2023-04-18 00:00:00", "2023-04-18 00:00:00"]
}

df = pd.DataFrame(data)
df["TIMESTAMP DATE"] = pd.to_datetime(df["TIMESTAMP DATE"])

# Transform the DataFrame
transformed_df = df.groupby(["ORIGEN", "TIMESTAMP DATE"]).size().reset_index(name="COUNT")

# Create a line chart using Plotly Express
fig = px.line(transformed_df, x="TIMESTAMP DATE", y="COUNT", color="ORIGEN")

# Show the plot
fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.