Code Monkey home page Code Monkey logo

Comments (1)

scls19fr avatar scls19fr commented on May 27, 2024

Probably not the best solution... but it can give some ideas for a possible implementation of a transpose method for DataFrame.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>MWE</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
  <body>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/bundle.min.js"></script>
    <script>
      (function(window, document, undefined) {
        // code that should be taken care of right away
        window.onload = init;

        function transpose(df) {
          array = df.values;
          transposed_array = array[0].map((_, colIndex) => array.map(row => row[colIndex]));
          return new dfd.DataFrame(transposed_array, {index: df.columns, columns: df.index});
        }

        function init() {
          console.log("init");
          json_data = [{ A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
            { A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
            { A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
            { A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 }];

          df = new dfd.DataFrame(json_data, {index: [10, 11, 12, 13]});
          df.print();

          transpose(df).print();
        }
      })(window, document, undefined);
    </script>
    <h1>MWE</h1>
  </body>
</html>

here is console output

╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ A                 │ B                 │ C                 │ D                 ║
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 10         │ 0.4612            │ 4.28283           │ -1.509            │ -1.1352           ║
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 11         │ 0.5112            │ -0.22863          │ -3.39059          │ 1.1632            ║
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 12         │ 0.6911            │ -0.82863          │ -1.5059           │ 2.1352            ║
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 13         │ 0.4692            │ -1.28863          │ 4.5059            │ 4.1632            ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

startup:4 
╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║            │ 10                │ 11                │ 12                │ 13                ║
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ A          │ 0.4612            │ 0.5112            │ 0.6911            │ 0.4692            ║
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ B          │ 4.28283           │ -0.22863          │ -0.82863          │ -1.28863          ║
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ C          │ -1.509            │ -3.39059          │ -1.5059           │ 4.5059            ║
╟────────────┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ D          │ -1.1352           │ 1.1632            │ 2.1352            │ 4.1632            ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

from danfojs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.