def write_dataframe(df, args):
    """
    Write a DataFrame to location specified in commandline arguments,
    optionally filtered by specific columns
    """
    assert len(args.output) > 0
    if args.output_columns is not None and len(args.output_columns) > 0:
        valid_columns = set(df.columns)
        for col in args.output_columns:
            if col not in valid_columns:
                raise ValueError("Column not found '%s', valid options: %s" %
                    (col, list(df.columns)))
        df = df[args.output_columns]
    df.to_csv(args.output, index=False)
