Filter rows by string pattern
Creates a new data frame containing only rows where the specified character column contains the given pattern
@param[in] df The source data frame @param[in] col_index Index of the character column to filter by @param[in] pattern String pattern to search for @return filtered_df New data frame with filtered rows
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(data_frame), | intent(in) | :: | df | |||
integer, | intent(in) | :: | col_index | |||
character(len=*), | intent(in) | :: | pattern |
function df_filter_rows_string_pattern(df, col_index, pattern) result(filtered_df) type(data_frame), intent(in) :: df integer, intent(in) :: col_index character(len=*), intent(in) :: pattern type(data_frame) :: filtered_df integer, dimension(:), allocatable :: selected_rows integer :: i, count, idx character(len=:), allocatable :: val type(column) :: col col = df % get_data_col(col_index) if (col % get_type() /= CHARACTER_NUM) error stop "column is not character type" ! Count matching rows count = 0 do i = 1, df % nrows() val = col % getch(i) if (index(val, pattern) > 0) count = count + 1 end do ! Collect matching row indices allocate (selected_rows(count)) idx = 0 do i = 1, df % nrows() val = col % getch(i) if (index(val, pattern) > 0) then idx = idx + 1 selected_rows(idx) = i end if end do ! Create filtered dataframe call filtered_df % new(df % get_max_char_len()) do i = 1, df % ncols() call copy_filtered_column(df, filtered_df, i, selected_rows) end do deallocate (selected_rows) end function df_filter_rows_string_pattern