Chris Tarn
Chris Tarn

Reputation: 629

Select a subset of columns (ie. filter data frame by collection of column keys)

There are some examples of how to get a single column from a data frame like this.

people?Age
people.["Age"]
people.GetColumn("Countries")

but how can you get a subset of multiple columns? For example, I would like to be able to do something like:

people.[["age";"Countries"]]  

or

people.GetColumn(["age";"Countries"])

and have it return a data frame with just those two columns/series. Is there a simple way to do this?

Upvotes: 2

Views: 1289

Answers (2)

Tomas Petricek
Tomas Petricek

Reputation: 243106

Using Frame.sliceCols is one way to do this, but you can use the slicing syntax too. The trick is to use slicing on people.Columns rather than directly on the frame:

people.Columns.[["age";"Countries"]]  

The motivation for this is that you might want to slice the rows as well, in which case you can do people.Rows.[ [1;3;5 ]. If your frame has ordered row index, you can select a range and do people.Rows.[1 .. 10].

Upvotes: 2

Chris Tarn
Chris Tarn

Reputation: 629

I found what I was looking for in the frame API section. One can select multiple columns, creating a new frame by:

let newFrame = people |> Frame.sliceCols(["age";"Countries"])

I think the current documentation is great, but it might benefit by adding an example like this to the section Manipulating data frames of this page.

Upvotes: 1

Related Questions