【问题标题】:How to change different values within dataframe如何更改数据框中的不同值
【发布时间】:2020-10-14 20:37:22
【问题描述】:

我有一个如下所示的数据框:

每个品牌都有不同的类别。我需要一个显示不同品牌的按钮。如果我单击该品牌,例如 bmw,我会在折线图中显示 4 个不同的类别。 x 轴是日期,y 轴是价格,线条的颜色按该品牌内的类别显示。 这是数据框。这些只是众多值中的 5 个

{'Date': {0: Timestamp('2020-03-18 00:00:00'),
  1: Timestamp('2020-03-18 00:00:00'),
  2: Timestamp('2020-03-18 00:00:00'),
  3: Timestamp('2020-03-18 00:00:00'),
  4: Timestamp('2020-03-18 00:00:00')},
 'price': {0: 281435.0, 1: 102577.0, 2: 204844.0, 3: 271199.0, 4: 144790.0},
 'Brand': {0: 'bmw', 1: 'ford', 2: 'hyundai', 3: 'mercedes-benz', 4: 'nissan'},
 'category': {0: 'bmw_5 series_executive_2016',
  1: 'ford_focus_trend x_2015',
  2: 'hyundai_tucson_elite plus_2017',
  3: 'mercedes-benz_e-class_edition e_2015',
  4: 'nissan_qashqai_black edition_2014'}}

【问题讨论】:

  • 你想做一个dash应用吗?
  • @rpanai 是的.....
  • 您已收到一个答案,如果您觉得它对您自己有用,请考虑给予支持或接受该答案。如果还不行,请留言。

标签: python pandas plotly


【解决方案1】:
# imports

from jupyter_dash import JupyterDash 
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd

# app
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = JupyterDash(__name__, external_stylesheets=external_stylesheets)

server = app.server

brands = df['Brand'].unique()  ## get unique brands for dropdown

app.layout = html.Div([
    html.Div(dcc.Dropdown(
            id='filter-brands',
            options=[{'label': i, 'value': i} for i in brands],
            value='bmw'
        ), 
         style={'display': 'inline-block',
                'width': "100%",
                'borderBottom': 'thin lightgrey solid',
                'backgroundColor': 'rgb(250, 250, 250)',
                'padding': '10px 5px'}),
    html.Div(dcc.Graph(
            id='categories-timeseries'
        ))
])

# to create timeseries chart on update
def create_time_series(dff):
    return px.line(dff, x="Date", y="price", color='category')


@app.callback(
    dash.dependencies.Output('categories-timeseries', 'figure'),
    dash.dependencies.Input('filter-brands', 'value'))
def update_timeseries(brand):
    dff = df[df['Brand'] == brand].sort_values("Date")
    return create_time_series(dff)


app.run_server(mode="jupyterlab")

或使用

app.run_server() ## to open in browser

【讨论】:

    猜你喜欢
    • 2019-12-12
    • 1970-01-01
    • 2017-12-17
    • 2018-02-20
    • 2020-10-18
    • 1970-01-01
    • 2022-01-05
    • 2020-08-05
    • 1970-01-01
    相关资源
    最近更新 更多