如何将普通数据框掩盖到烛台数据框架中?

发布于 2025-01-26 16:25:07 字数 1304 浏览 4 评论 0 原文

fig = go.Figure()
fig.add_trace(
  go.Scatter(x=alldates, y=countriesData[0]["ToxPi Score"], 
  name="Suffolk",customdata=countriesData[0],hovertemplate=common_template)
)
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
    buttons=list([
        dict(count=1, label="1m", step="month", stepmode="backward"),
        dict(count=6, label="6m", step="month", stepmode="backward"),
        dict(count=1, label="YTD", step="year", stepmode="todate"),
        dict(count=1, label="1y", step="year", stepmode="backward"),
        dict(step="all")
    ])
 )
 )

fig.show()

“在此处输入映像”

我的数据框架看起来像这样

我正在研究一个简单的数据集,在该数据集中,我根据日期绘制了状态的TOXPI分数。

现在,我需要根据给定的方向将这些数据绘制为烛台格式

开放式值:在给定7天的给定范围内的第一天TOXPI值。 接近值:在给定范围为7 天。 高值:在给定7天的给定范围内的最高毒素值。 低值:给定7天的最低毒品值。

我无法弄清楚如何掩盖它或将数据分为每周格式

编辑

看起来像这样

请帮忙 提前。

fig = go.Figure()
fig.add_trace(
  go.Scatter(x=alldates, y=countriesData[0]["ToxPi Score"], 
  name="Suffolk",customdata=countriesData[0],hovertemplate=common_template)
)
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
    buttons=list([
        dict(count=1, label="1m", step="month", stepmode="backward"),
        dict(count=6, label="6m", step="month", stepmode="backward"),
        dict(count=1, label="YTD", step="year", stepmode="todate"),
        dict(count=1, label="1y", step="year", stepmode="backward"),
        dict(step="all")
    ])
 )
 )

fig.show()

enter image description here

My dataframe looks like this

I am working on a simple dataset in which i have plotted ToxPi score of a state according to dates.

Now i need to plot this data into candlestick format according to given directions

Open value: ToxPi value at the first day in a given range of 7 days.
Close value: ToxPi value at the last (7th) day in a given range of 7
days.
High value: The highest ToxPi value in a given range of 7 days.
Low value: The lowest ToxPi value in a given range of 7 days.

I can't figure out the way how to covert it or separate the data into weekly format

EDIT

After updating datetime column in df it looks like this

Please Help
Thanx in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2025-02-02 16:25:07

尽管我对“给定7天的范围”的含义并不完全清楚,但我的猜测是应该使用7天的滚动计算来计算开放,高,低和关闭,并且您的数据具有谷物1天。

我不确定您的 nestoryData [0] 有一个DateTime列,因此我们将使用 alldates 您传递给 go.scatter go.scatter :

df = countriesData[0].copy()
df["date_time"] = alldates
df["date_time"] = pd.to_datetime(df["date_time"])
df = df.set_index("date_time")

## create your features Open, Close, High, Low:
df["Open"] = df["ToxPi Score"].rolling("7D").apply(lambda row: row.iloc[0])
df["Close"] = df["ToxPi Score"].rolling("7D").apply(lambda row: row.iloc[-1])
df["High"] = df["ToxPi Score"].rolling("7D").max()
df["Low"] = df["ToxPi Score"].rolling("7D").max()

然后,您可以使用修改后的数据框来构建烛台图:

fig = go.Figure(data=[go.Candlestick(
    x=df.index,
    open=df["Open"],
    high=df["High"],
    low=df["Low"],
    close=df["Close"]
)])

Although I am not completely clear in what is meant by a "given range of 7 days", my guess is that a 7 day rolling calculation should be used to calculate open, high, low, and close, and that your data has a grain of 1 day.

I am not sure if your countriesData[0] has a datetime column, so we'll use the alldates array you passed to go.Scatter:

df = countriesData[0].copy()
df["date_time"] = alldates
df["date_time"] = pd.to_datetime(df["date_time"])
df = df.set_index("date_time")

## create your features Open, Close, High, Low:
df["Open"] = df["ToxPi Score"].rolling("7D").apply(lambda row: row.iloc[0])
df["Close"] = df["ToxPi Score"].rolling("7D").apply(lambda row: row.iloc[-1])
df["High"] = df["ToxPi Score"].rolling("7D").max()
df["Low"] = df["ToxPi Score"].rolling("7D").max()

Then you can use your modified DataFrame to construct a candlestick chart:

fig = go.Figure(data=[go.Candlestick(
    x=df.index,
    open=df["Open"],
    high=df["High"],
    low=df["Low"],
    close=df["Close"]
)])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文