如何使表格/电子表格(例如 Pandas DataFrame)可观察、使用触发器或更改事件? [英] How to make tables/spreadsheets (e.g. pandas DataFrame) observable, use triggers or change events?

查看:82
本文介绍了如何使表格/电子表格(例如 Pandas DataFrame)可观察、使用触发器或更改事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 Pandas Dataframes 可见?

Is there a way to make pandas Dataframes observable?

假设我有一些数据帧 A、B、C 具有相同的索引,而 C 的计算方法是

Lets say I have some Dataframes A, B, C that have the same index and C is calculated by

C = A + B

如果我更改 A 中的单元格值(例如 2 => 4),我想自动更新 C 中的相应单元格(例如 22 => 24).

If I change a cell value in A (e.g. 2 => 4), I would like to automatically update the corresponding cell in C (e.g. 22 => 24).

=>如何监听 Dataframe A 中的变化?

=> How can I listen to changes in Dataframe A?

是否有一些我可以使用的可观察数据框、触发器或事件?如果 Pandas Dataframes 无法做到这一点,我可以使用其他一些可观察的表结构吗?

Is there some observable Dataframe, triggers or events I could use for that? If that is not possible with pandas Dataframes, is there some other observable table structure I could use?

或者我是否需要实现自己的电子表格?

Or would I need to implement my own spreadsheets?

其他信息:

我的表的索引可能包含多列.也可能有多个值列.

The index of my tables might consist of multiple columns. There might be multiple value columns, too.

如果我可以将该数据结构与 RxPy 一起使用,那就太好了,这样我就可以为表格计算创建反应管道.

It would be great if I could use that data structure together with RxPy, so that I can create reactive pipes for table calculations.

我的计算管道/链可能包含数百个表.这就是我不想使用 Excel 的原因.

My calculation pipes/chains might contain hundreds of tables. That's why I don't want to use Excel.

如果用 Python 做这样的事情很难,我也欢迎基于 JavaScript 的解决方案.

If it is hard to do such things in Python, I am also open for solutions based on JavaScript.

相关内容

这是我目前找到的一些信息.PyQt 表可能是一种方法,因为它们支持更改事件.但是,我正在寻找开销更少"的东西.供我计算.

Here is some information I found so far. PyQt tables might be a way to go, because they support change events. However, I am looking for something with "less overhead" for my calculations.

https://pythonspot.com/pyqt5-table/

python 中的 Observer Observable 类

Python 中的事件系统

推荐的实现可观察集合/序列的方法在 Python 中?

推荐答案

不是 Python 的解决方案,但我发现了一些很有前途的 JavaScript 库:

Not a solution for Python, but I found some promising JavaScript libraries:

  • RxDb https://rxdb.info/ "A realtime Database for JavaScript Applications"

RxJs https://rxjs.dev/guide/overview "RxJS 是一个使用可观察序列编写异步和基于事件的程序的库."

RxJs https://rxjs.dev/guide/overview "RxJS is a library for composing asynchronous and event-based programs by using observable sequences."

基于这些库,我在这里试验了可观察表的管道:

Based on that libraries I am experimenting with pipes for observable tables here:

https://github.com/stefaneidelloth/rxtable

import * as d3 from 'd3';
import { range, Observable } from 'rxjs';

import DatabaseFactory from './database/databaseFactory';
import KeyContext from './table/keyContext';
import DataContext from './table/dataContext';
import { add } from './operator/operators';

run();

async function run() {
  const databaseFactory = new DatabaseFactory();
  const database = await databaseFactory.create('project');

  const keyContext = new KeyContext(['scenario_id','country_id']);
  const dataContext = new DataContext(['y2020','y2030','y2040']);

  const inputTable = await database.createTable('input', keyContext, dataContext);

  const inputRow = {
    scenario_id: 0,
    country_id: 0,
    y2020: 1,
    y2030: 2,
    y2040: 3,
  };

  let newValue = 4;
  async function buttonClicked() {
    inputRow.y2040 = newValue;
    await inputTable.update(inputRow);
    newValue++;
  }

  d3.select('#root')
    .append('button')
    .text('Click me')
    .on('click', () => buttonClicked());

  // doc for Observable API:
  // https://rxjs.dev/api/index/class/Observable

  // doc for pipe-able operators:
  // https://rxjs.dev/guide/operators
  // https://rxjs.dev/api/operators
  inputTable.pipe(
    add('sum', 66)
  )
  .subscribe({
    initialized(table) {
      console.log(`table: ${table.name}`);
    },
    rowAdded(newRow) {
      console.log(`rowAdded`);
    },
    rowChanged(oldRow, newRow) {
      console.log(`rowChanged`);
    },
    error(error) { console.error(`error: ${error}`); },
    complete() { console.log('done'); },
  });

  await inputTable.push(inputRow);

  await inputTable.push({
    scenario_id: 1,
    country_id: 1,
    y2020: 11,
    y2030: 12,
    y2040: 13,
  });

}

我仍然对 Python 的解决方案感兴趣.如果你知道一个,请告诉我.对于那些更喜欢 JavaScript 而不是 Python 的人来说,有人已经开始将 pandas 移植到 JavaScript 可能会很有趣:https://stratodem.github.io/pandas.js-docs/#introduction.

I am still interested in solutions for Python, too. If you know one, please let me know. For those who prefer JavaScript over Python, it might be interesting that someone already started to port pandas to JavaScript: https://stratodem.github.io/pandas.js-docs/#introduction.

这篇关于如何使表格/电子表格(例如 Pandas DataFrame)可观察、使用触发器或更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆