SOLVED

Best practice to fill one column with manipulated values from another (DataFrame)?

New Contributor
C# 10 / .NET 6 / Microsoft.Data.Analysis

 

Given a Microsoft.Data.Analysis DataFrame with two columns, what is the idiomatic way to take values from one column, manipulate them, then use the resulting value to fill the rows of the second column element-wise?

        // Create a DateTime column.
        PrimitiveDataFrameColumn<DateTime> dateTimeCol = new("Dates", 0);
        // Fill it.
        dateTimeCol.Append(DateTime.Now + TimeSpan.FromDays(1));
        dateTimeCol.Append(DateTime.Now + TimeSpan.FromDays(2));
        dateTimeCol.Append(DateTime.Now + TimeSpan.FromDays(3));

        // Create a Ticks column.
        PrimitiveDataFrameColumn<long> ticksCol = new("Ticks", dateTimeCol.Length);

        // Create a DataFrame of the above two columns.
        DataFrame df = new();
        df.Columns.Add(dateTimeCol);
        df.Columns.Add(ticksCol);

At this point, what I want to do is df["Ticks"] = df["Dates"].Ticks. Of course, this doesn't work. I could do this:

        for (int i = 0; i < df.Rows.Count; i++)
        {
            DateTime tempDate = (DateTime) df[i, df.Columns.IndexOf("Dates")];
            df[i, df.Columns.IndexOf("Ticks")] = tempDate.Ticks;
        }

But... is there a better way?

4 Replies
best response confirmed by chrisxfire (New Contributor)
Solution

@chrisxfire not sure if this is what you were looking for, but you might be able to use the Apply function to take the values from one column, manipulate them, and save them to another column. Here's an example of that:

 

LuisQuintanilla_0-1651625425845.png

 

Alternatively, using the same data as above, you could also do the following which produces the same result:

LuisQuintanilla_0-1651625837724.png

I believe that the `Apply` method is implemented on PrimitiveDataFrameColumn, but not DataFrameColumn which is why you can't call it directly from `df["Numbers"]`

 

 

This is very helpful Luis, thank you.

May I ask what code editor you are using in those screenshots?
I'm in Visual Studio using the Notebook Editor extension. Feel free to check it out and give us feedback! :)

https://marketplace.visualstudio.com/items?itemName=MLNET.notebook
Ah, this brings me back to my Python days! Installing now... thanks!