Sunday, November 17, 2024
Google search engine
HomeLanguagesPandas DataFrame.to_sparse() Method

Pandas DataFrame.to_sparse() Method

Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure of the Pandas

Pandas DataFrame.to_sparse

Pandas DataFrame.to_sparse() function convert to SparseDataFrame. The function implements the sparse version of the DataFrame meaning that any data matching a specific value it’s omitted in the representation. The sparse DataFrame allows for more efficient storage.

Syntax: DataFrame.to_sparse(fill_value=None, kind=’block’) 

Parameter : 

  • fill_value : The specific value that should be omitted in the representation. 
  • kind : {‘block’, ‘integer’}, default ‘block’ 

Returns : SparseDataFrame

Pandas SparseDataFrame Example

Example 1: Use DataFrame.to_sparse() function to convert the given Dataframe to a SparseDataFrame for efficient storage. 

Python3




# importing pandas as pd
import pandas as pd
 
# Creating the DataFrame
df = pd.DataFrame({'Weight': [45, 88, 56, 15, 71],
                   'Name': ['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],
                   'Age': [14, 25, 55, 8, 21]})
 
# Create the index
index_ = pd.date_range('2010-10-09 08:45', periods=5, freq='H')
 
# Set the index
df.index = index_
 
# Print the DataFrame
print(df)


Output : 

 

 Now we will use DataFrame.to_sparse() function to convert the given dataframe to a SparseDataFrame. 

Python3




# convert to SparseDataFrame
result = df.to_sparse()
 
# Print the result
print(result)
 
# Verify the result by checking the
# type of the object.
print(type(result))


Output : 

 

 

 

As we can see in the output, the DataFrame.to_sparse() function has successfully converted the given dataframe to a SparseDataFrame type.   

Example 2: Use DataFrame.to_sparse() function to convert the given Dataframe to a SparseDataFrame for efficient storage. 

Python3




# importing pandas as pd
import pandas as pd
 
# Creating the DataFrame
df = pd.DataFrame({"A": [12, 4, 5, None, 1],
                   "B": [7, 2, 54, 3, None],
                   "C": [20, 16, 11, 3, 8],
                   "D": [14, 3, None, 2, 6]})
 
# Create the index
index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5']
 
# Set the index
df.index = index_
 
# Print the DataFrame
print(df)


Output : 

 

Now we will use DataFrame.to_sparse() function to convert the given dataframe to a SparseDataFrame. 

Python3




# convert to SparseDataFrame
result = df.to_sparse()
 
# Print the result
print(result)
 
# Verify the result by checking the
# type of the object.
print(type(result))


Output : 

 

 

 As we can see in the output, the DataFrame.to_sparse() function has successfully converted the given Dataframe to a SparseDataFrame type.

RELATED ARTICLES

Most Popular

Recent Comments