Mostafa
Mostafa

Reputation: 1561

Pandas: how to subtract the mean column of a sparse dataframe?

In pandas 0.25.3, I can't subtract the mean column of a sparse dataframe. I tried:

df_norm= df.sub(df.mean(axis=1), axis=0)

but I get:

AttributeError: module 'pandas._libs.sparse' has no attribute 'sparse_sub_float32'

Upvotes: 0

Views: 212

Answers (1)

Poe Dator
Poe Dator

Reputation: 4912

You may have this error if you use older Pandas SparseDataFrame structure. However in latest Pandas (I use 0.25.3) there is a different way to deal with sparse and your code works fine. Consider converting your dataframe into the modern sparse type. See https://pandas.pydata.org/pandas-docs/stable/user_guide/sparse.html#sparse-migration. Below is a working example

df = pd.DataFrame (np.random.randint(0,100,(5,5)))
dtype = pd.SparseDtype(int, fill_value=0)   # define sparse type
df = df.astype(dtype)  # to new sparse type
print(df.sub(df.mean(axis=1), axis=0))

      0     1     2     3     4
0 -28.6  43.4 -42.6 -14.6  42.4
1  29.4  25.4 -48.6  -4.6  -1.6
2 -12.4  52.6 -15.4 -17.4  -7.4
3 -20.8  -2.8  10.2  -6.8  20.2
4  35.8  -4.2  19.8 -16.2 -35.2

Upvotes: 1

Related Questions