Skip to content

Series to DataFrame #619

Open
Open

Description

@s-celles

Hello,

I don't know if it's a feature request or doc request... but I haven't found how to convert a Series to a DataFrame.
Similar to https://pandas.pydata.org/docs/reference/api/pandas.Series.to_frame.html

Kind regards

Activity

  1. s-celles commented on Nov 20, 2023

    @s-celles
    Author

    This is probably not the best approach but here is a solution

    <!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/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
      <body>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.min.js"></script>
        <script>
          (function(window, document, undefined) {
            // code that should be taken care of right away
            window.onload = init;
    
            function row2col(row_array) {
              col_array = []
              row_array.forEach(function(row) {
                col_array.push([row])
              })
              return col_array;
            }
    
            function toframe(ser, colname) {
              df = new dfd.DataFrame(row2col(ser.values), { columns: [colname], index: ser.index });
              return df
            }
    
            function init() {
              console.log("init");
              ser = new dfd.Series([10, 11, 12, 13], { index: ["a", "b", "c", "d"] });
              ser.print();
              df = toframe(ser, "NewCol");
              df.print()
            }
          })(window, document, undefined);
        </script>
        <h1>MWE</h1>
      </body>
    </html>

    and console ouput

    init
    startup:4 
    ╔═══╤════╗
    ║ a │ 10 ║
    ╟───┼────╢
    ║ b │ 11 ║
    ╟───┼────╢
    ║ c │ 12 ║
    ╟───┼────╢
    ║ d │ 13 ║
    ╚═══╧════╝
    
    startup:4 
    ╔════════════╤═══════════════════╗
    ║            │ NewCol            ║
    ╟────────────┼───────────────────╢
    ║ a          │ 10                ║
    ╟────────────┼───────────────────╢
    ║ b          │ 11                ║
    ╟────────────┼───────────────────╢
    ║ c          │ 12                ║
    ╟────────────┼───────────────────╢
    ║ d          │ 13                ║
    ╚════════════╧═══════════════════╝
    

    Maybe Series could have a method for that.

    Any opinion?

  2. s-celles commented on Nov 20, 2023

    @s-celles
    Author

    Moreover if Series had a name attribute we could, by default use it to give DataFrame column an apropriate name

  3. bml1g12 commented on Apr 18, 2024

    @bml1g12

    Moreover, in typescript, I'm unclear how to type narrow something to being a DataFrame, when we know it is not a Series

    df.constructor.name === "DataFrame"
    df instanceof dfd.DataFrame

    Both seem to be giving me issues

  4. risenW commented on Apr 2, 2025

    @risenW
    Member

    We should definitely have a method for this to convert Series to DataFrame. Thanks for raising, will triage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions