I want to add an empty sparse Pandas series to my dataframe. I tried the following but the resulting series is neither sparse, nor of the datatype I intended:
df = pd.DataFrame({"a":[1,2,3]})
df['newcolumn'] = pd.Series(dtype='bool_').to_sparse(fill_value=False)
This adds the 'newcolumn' series to my dataframe but the result looks like this:
a newcolumn
0 1 NaN
1 2 NaN
2 3 NaN
And newcolumn looks like this:
0 NaN
1 NaN
2 NaN
Name: newcolumn, dtype: object
What I want to see is:
0 False
1 False
2 False
Name: newcolumn, dtype: bool
I can't figure out how to add this without losing my data type and maintaining sparseness.
I recognize there are roundabout ways to achieve this, but I'll be working with very large datasets and I want to avoid creating unnecessary objects in memory.
Per @COLDSPEED's comment, it's not possible to combine a sparse series with a dense dataframe. The solution is to convert the dataframe to be sparse and then add the series.
df = df.to_sparse()
df['newcol'] = pd.SparseSeries([False] * len(df), dtype='bool_', fill_value=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.