'Pause observables for a certain period of time when a condition is meet

I'm studying how to pause a flow of observables when a certain condition is meet; essentially, in this example, there is an observable, an inner observable and a subject. The observable and the inner observable must be pause for five seconds when the subject emit the value 2; the problem here is that the timeout occurs only at the end of the computation.

import { from, interval, map, mergeMap, Subject } from "rxjs";

let subjectCounter: number = 0;
const subjectNumbers$ = new Subject<number>();

const myObservable1$ = from(["Venus", "Jupiter", "Mars"]);
const myObservable2$ = from(["Red", "Green", "Blue"]);

const myObservable1Pipeline = myObservable1$.pipe(
  map((name) => {
    // ***************subject*********************
    subjectNumbers$.subscribe({
      next: (mySubjectNumber) => {
        console.log(
          `From subjectNumbers$ - mySubjectNumber: ${mySubjectNumber}`
        );
      },
    });
    console.log(`IN myObservable1$.pipe - name: ${name}`);
    // ***************end subject*********************

    // ***************observable2*********************
    const myObservable2Observer = {
      next: (colour: string) => {
        subjectNumbers$.next(subjectCounter++);
        console.log(
          `From myObservable2Observer - subjectCounter: ${subjectCounter}`
        );
        console.log(`From myObservable2$ - colour: ${colour}`);
        if (subjectCounter === 2) {
          setTimeout(() => {
            console.log(`Timeout - subjectCounter: ${subjectCounter}`);
          }, 5000);
        }
      },
      error: () => {
        console.log(`ERROR IN myObservable2$`);
        myObservable2Subscription.unsubscribe();
      },
    };

    const myObservable2Subscription = myObservable2$.subscribe(
      myObservable2Observer
    );
    return name;
  })
  // ***************end observable2*********************
);

const myObservable1Subscription = myObservable1Pipeline.subscribe({
  next: (name) => {
    console.log(
      `-->From myObservable1Subscription - name: ${name} - subjectCounter: ${subjectCounter}`
    );

    if (subjectCounter === 2) {
      setTimeout(() => {
        console.log(`Timeout - subjectCounter: ${subjectCounter}`);
      }, 5000);
    }
  },
  error: () => {
    console.log(`ERROR in myObservable1Subscription`);
    myObservable1Subscription.unsubscribe();
  },
});


Solution 1:[1]

Try to parse your strings (a list of lists) then create your dataframe from the real list:

import pandas as pd
import re

s = '[[A,1], [B,5], [C,18]]'

cols = ['Category', 'Values']
data = [row.split(',') for row in re.findall('\[([^]]+)\]', s[1:-1])]
df = pd.DataFrame(data, columns=cols)
print(df)

# Output:
  Category Values
0        A      1
1        B      5
2        C     18

Solution 2:[2]

You should be able to just use pandas.DataFrame and pass in your data, unless I'm misunderstanding the question. Anyway, try:

df = pandas.DataFrame(data=d, columns = ['Category', 'Value'])

where d is your list of tuples.

Solution 3:[3]

from prettytable import PrettyTable

column = [["A",1],["B",5],["C",18]]

columnname=[]
columnvalue =[]
t = PrettyTable(['Category', 'Values'])

for data in column:
    columnname.append(data[0])
    columnvalue.append(data[1])
    t.add_row([data[0], data[1]])
    
print(t)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien
Solution 2 Bashton
Solution 3 Clarence Chhoa