convert pandas dataframe to sparse

Solutions on MaxInterview for convert pandas dataframe to sparse by the best coders in the world

showing results for - "convert pandas dataframe to sparse"
Jacopo
21 Mar 2016
1def convert_to_sparse_pandas(df, exclude_columns=[]):
2    """
3    Converts columns of a data frame into SparseArrays and returns the data frame with transformed columns.
4    Use exclude_columns to specify columns to be excluded from transformation.
5    :param df: pandas data frame
6    :param exclude_columns: list
7        Columns not be converted to sparse
8    :return: pandas data frame
9    """
10    df = df.copy()
11    exclude_columns = set(exclude_columns)
12
13    for (columnName, columnData) in df.iteritems():
14        if columnName in exclude_columns:
15            continue
16        df[columnName] = pd.SparseArray(columnData.values, dtype='uint8')
17
18    return df