<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def intersect(df, other, index=False, keep='first'):
 """ Returns rows that appear in both DataFrames. Args: df (pandas.DataFrame):
 data passed in through the pipe. other (pandas.DataFrame):
 other DataFrame to use for set operation with the first. Kwargs: index (bool):
 Boolean indicating whether to consider the pandas index as part of the set operation (default `False`). keep (str):
 Indicates which duplicate should be kept. Options are `'first'` and `'last'`. """


    validate_set_ops(df, other)
    if index:
        df_reset_index = df.reset_index()
        other_reset_index = other.reset_index()
        index_cols = [col for col in df_reset_index.columns if col not in df.columns]
        df_index_names = df.index.names
        return_df = (pd.merge(df_reset_index, other_reset_index,
                              how='inner',
                              left_on=df_reset_index.columns.values.tolist(),
                              right_on=df_reset_index.columns.values.tolist())
                     .set_index(index_cols))
        return_df.index.names = df_index_names
        return_df = return_df.drop_duplicates(keep=keep)
        return return_df
    else:
        return_df = pd.merge(df, other,
                             how='inner',
                             left_on=df.columns.values.tolist(),
                             right_on=df.columns.values.tolist())
        return_df = return_df.drop_duplicates(keep=keep)
        return return_df