Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transpose a DataFrame #618

Open
scls19fr opened this issue Nov 18, 2023 · 1 comment
Open

Transpose a DataFrame #618

scls19fr opened this issue Nov 18, 2023 · 1 comment

Comments

@scls19fr
Copy link

Hello,

it would be nice to add a transpose method to DanfoJS DataFrame similar to https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.transpose.html

Kind regards

@scls19fr
Copy link
Author

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            ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant