Combine expression columns, calculating the mean for 2 columns


    :param df: Pandas dataframe
    :param columns_to_combine: A list of tuples containing the column names to combine
    :return:
503854
def combine_expression_columns(df, columns_to_combine, remove_combined=True):

    

    df = df.copy()

    for ca, cb in columns_to_combine:
        df["%s_(x+y)/2_%s" % (ca, cb)] = (df[ca] + df[cb]) / 2

    if remove_combined:
        for ca, cb in columns_to_combine:
            df.drop([ca, cb], inplace=True, axis=1)

    return df
488071
def df_from_groups(groups, columns=None):
    """"""
    if columns is None:
        columns = list(groups.get_group(groups.indices.keys()[0]).columns)
    df = pd.DataFrame()
    for col, group_label in product(columns, groups.indices.keys()):
        label = '{}_{}'.format(col, group_label)
        df[label] = pd.Series(groups.get_group(group_label)[col].values)
    return df
141597
def aggregate(all_stats):
    
    aggregate_stats = {'means': [], 'standard_deviations': []}
    for optimizer_key in all_stats:
        # runs is the mean, for add_mean_sd function
        mean_stats = copy.deepcopy(all_stats[optimizer_key]['mean'])
        mean_stats['name'] = optimizer_key
        aggregate_stats['means'].append(mean_stats)

        # also keep track of standard deviations
        sd_stats = copy.deepcopy(
            all_stats[optimizer_key]['standard_deviation'])
        sd_stats['name'] = optimizer_key
        aggregate_stats['standard_deviations'].append(sd_stats)

    _add_mean_sd_to_stats(aggregate_stats, 'means')

    return aggregate_stats
706192
def expand_dates(df, columns=[]):
    
    columns = df.columns.intersection(columns)
    df2 = df.reindex(columns=set(df.columns).difference(columns))
    for column in columns:
        df2[column + '_year'] = df[column].apply(lambda x: x.year)
        df2[column + '_month'] = df[column].apply(lambda x: x.month)
        df2[column + '_day'] = df[column].apply(lambda x: x.day)
    return df2
1141322
def from_df(cls, df):
        """"""
        t = cls()
        labels = df.columns
        for label in df.columns:
            t.append_column(label, df[label])
        return t
205807
def correl(df,periods=21,columns=None,include=True,str=None,detail=False,how='value',**correl_kwargs):
	
	def _correl(df,periods=21,columns=None,include=True,str=None,detail=False,**correl_kwargs):
		study='CORREL'
		df,_df,columns=validate(df,columns)

		_df['CORREL'] = df[columns[0]].rolling(window=periods,**correl_kwargs).corr(df[columns[1]])

		str=str if str else 'CORREL({column1},{column2},{period})'.format(column1=columns[0],column2=columns[1],period=periods)
		return rename(df,_df,study,periods,columns,include,str,detail)
	columns=df.columns if not columns else columns
	if len(columns) != 2: 
		raise StudyError("2 Columns need to be specified for a correlation study")
	periods=make_list(periods)
	if how=='pct_chg':
		df=df[columns].pct_change()
	elif how=='diff':
		df=df[columns].diff()
	__df=pd.concat([_correl(df,columns=columns,periods=y,include=False,str=str) for y in periods],axis=1)
	if include:
		return pd.concat([df,__df],axis=1)
	else:
		return __df
198973
def inv_mix_columns(state):
    
    state = state.reshape(4, 4, 8)
    return fcat(
        multiply(IMA, state[0]),
        multiply(IMA, state[1]),
        multiply(IMA, state[2]),
        multiply(IMA, state[3]),
    )
949666
void combine(final MomentStatistics b) {
		requireNonNull(b);

		final double m2 = _m2.value();
		final double m3 = _m3.value();

		final double pn = _n;
		final double n = _n + b._n;
		final double nn = n*n;

		final double d = b._m1.value() - _m1.value();
		final double dd = d*d;

		_n += b._n;

		_m1.add(d*b._n/n);

		_m2.add(b._m2).add(dd*pn*b._n/n);

		_m3.add(b._m3)
			.add(dd*d*(pn*b._n*(pn - b._n)/nn))
			.add(3.0*d*(pn*b._m2.value() - b._n*m2)/n);

		final double bnbn = (double)b._n*(double)b._n;

		_m4.add(b._m4)
			.add(dd*dd*(pn*b._n*(pn*pn - pn*b._n + bnbn)/(nn*n)))
			.add(6.0*dd*(pn*pn*b._m2.value() + bnbn*m2)/nn)
			.add(4.0*d*(pn*b._m3.value() - b._n*m3)/n);
	}
73016
def discrete_columns(df, ignore):
    
    lst = []
    for col in df:
        if array_kind.discrete(df[col]) and (col not in ignore):
            # Some columns are represented as object dtype
            # but may have compound structures as values.
            try:
                hash(df[col].iloc[0])
            except TypeError:
                continue
            lst.append(col)
    return lst
328796
def mutating_join(*args, **kwargs):
  
  # candidate for improvement
  left = args[0]
  right = args[1]
  if 'by' in kwargs:
    left_cols, right_cols = get_join_cols(kwargs['by'])
  else:
    left_cols, right_cols = None, None
  if 'suffixes' in kwargs:
    dsuffixes = kwargs['suffixes']
  else:
    dsuffixes = ('_x', '_y')
  if left._grouped_on:
    outDf = (DplyFrame((left >> ungroup())
                       .merge(right, how=kwargs['how'], left_on=left_cols, 
                              right_on=right_cols, suffixes=dsuffixes))
             .regroup(left._grouped_on))
  else:
    outDf = DplyFrame(left.merge(right, how=kwargs['how'], left_on=left_cols, 
                                 right_on=right_cols, suffixes=dsuffixes))
  return outDf
1182105
def _set_column_filter_expressions(self):
        
        # per columns filters:
        for i in range(len(self.columns)):
            filter_expr = None
            value = self.params.get('columns[{:d}][search][value]'.format(i),
                                    '')
            if value:
                search_func = SEARCH_METHODS[self.columns[i].search_method]
                filter_expr = search_func(self.columns[i].sqla_expr, value)
            self.filter_expressions.append(filter_expr)
673151
def concat other_df
      vectors = (@vectors.to_a + other_df.vectors.to_a).uniq

      data = vectors.map do |v|
        get_vector_anyways(v).dup.concat(other_df.get_vector_anyways(v))
      end

      Daru::DataFrame.new(data, order: vectors)
    end
785461
def combine_dfs(dfs, names, method):
    """"""
    if method == "track":
        res = list()
        for df, identifier in zip(dfs, names):
            df["dataset"] = identifier
            res.append(df)
        return pd.concat(res, ignore_index=True)
    elif method == "simple":
        return pd.concat(dfs, ignore_index=True)
804891
def _to_df(self, query, conn, index_col=None, coerce_float=True, params=None,
               parse_dates=None, columns=None):
        
        return pd.io.sql.read_sql(str(query), conn, index_col=index_col,
                                  coerce_float=coerce_float, params=params,
                                  parse_dates=parse_dates, columns=columns)
574716
def _shape(self, df):
        

        row, col = df.shape
        return row + df.columns.nlevels, col + df.index.nlevels
1265165
def single_row_or_col_df_to_series(desired_type: Type[T], single_rowcol_df: pd.DataFrame, logger: Logger, **kwargs)\
        -> pd.Series:
    
    if single_rowcol_df.shape[0] == 1:
        # one row
        return single_rowcol_df.transpose()[0]
    elif single_rowcol_df.shape[1] == 2 and isinstance(single_rowcol_df.index, pd.RangeIndex):
        # two columns but the index contains nothing but the row number : we can use the first column
        d = single_rowcol_df.set_index(single_rowcol_df.columns[0])
        return d[d.columns[0]]
    elif single_rowcol_df.shape[1] == 1:
        # one column and one index
        d = single_rowcol_df
        return d[d.columns[0]]
    else:
        raise ValueError('Unable to convert provided dataframe to a series : '
                         'expected exactly 1 row or 1 column, found : ' + str(single_rowcol_df.shape) + '')
903070
def _combine(self, applied):
        """"""
        applied_example, applied = peek_at(applied)
        coord, dim, positions = self._infer_concat_args(applied_example)
        combined = concat(applied, dim)
        combined = _maybe_reorder(combined, dim, positions)
        if coord is not None:
            combined[coord.name] = coord
        combined = self._maybe_restore_empty_groups(combined)
        combined = self._maybe_unstack(combined)
        return combined