Python Dash: loading pandas dataframes into data table(Python Dash:将 pandas 数据帧加载到数据表)
本文介绍了Python Dash:将 pandas 数据帧加载到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最近我一直在尝试用Dash构建一个应用程序,但尽管看了很多指南,我就是想不出如何将 pandas 数据帧导入到Dash的数据表中(这本质上是 pandas 数据帧,除了网络托管和被动)。
大多数示例说明了如何手动从已在示例中硬编码的数据帧中挑选特定的列/行,如here中所示。然而,在我的情况下,数据帧是在我的代码中构建的( pandas 是实现这一点的最简单的方法),所以我最终不得不想出一种方法将pd.Dataframe()转换为dash_table.DataTable()。
我如何才能使其工作?使用引用,我尝试了以下代码将我的数据帧的字典发送到dash_table.DataTable(),但没有显示任何内容。
我的代码:
## Imports
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output, State
import datetime as dt
import pandas as pd
import numpy as np
## Custom functions that creates the pandas dataframe
from twitter_functions import old_tweets
app = dash.Dash(dev_tools_hot_reload=True)
app.scripts.config.serve_locally = True
app.config['suppress_callback_exceptions'] = True
app.layout = html.Div(children=[
html.H3('Twitter App'),
dcc.Input('ScreenName_Input', type='text'),
html.Button(id='screenNames_submit_button', children='Submit'),
dash_table.DataTable(id='tweet_table')
])
@app.callback(
Output(component_id='tweet_table', component_property='data'),
[Input(component_id='screenNames_submit_button', component_property='n_clicks_timestamp')],
[State(component_id='ScreenName_Input', component_property='value')]
)
def display_tweets(submit_button, screen_names):
tweets = old_tweets(screen_names)
return tweets.to_dict(orient='records')
if __name__ == '__main__':
app.run_server(debug=True)
推荐答案
在someone也在策略性论坛上回复我之后(谢天谢地),似乎最终的答案是在你的数据表中预先设置好要在某个时候进入的 pandas 数据框的列,就像这样,
dash_table.DataTable(
id='table',
columns=[
{'name': 'Column 1', 'id': 'column1'},
{'name': 'Column 2', 'id': 'column2'},
{'name': 'Column 3', 'id': 'column3'},
{'name': 'Column 4', 'id': 'column4'},
{'name': 'Column 5', 'id': 'column5'}]
)
,然后发送您的 pandas 数据帧的字典。
这篇关于Python Dash:将 pandas 数据帧加载到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:Python Dash:将 pandas 数据帧加载到数据表
基础教程推荐
猜你喜欢
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
