How to select rows from a dataframe and then create a dataframe with multiple columns from the selection

Hi all
noob here

I can select the rows I want from a dataframe and create a new data using ONE of the original dataframes columns BUT how do I add TWO or more please?

so if test_data has columns col1,col2,col3,col4,col5

and this creates df_test with all the col1 with “TEST” in it.

df_test  = test_data[test_data.col1 .== "TEST", :].col1

how do I add col3 and col5 to df_test using the same line?

I tried

df_test = test_data[test_data.col1 .== "TEST", :].[col3, col5]

and it didn’t work.thanks for any help

df_test = test_data[test_data.col1 .== "TEST", ["col1", "col3", "col5"]]

or if you prefer to generate column names dynamically

df_test = test_data[test_data.col1 .== "TEST", string.("col", 1:2:5)]

In general data frame indexing has the following form:

source_data_frame[row_selector, column_selector]
1 Like

thank you so much. Worked like a champ. Really helpful answer.