Metadata, also known as data about the data. Metadata can give us data description, summary, storage in memory, and datatype of that particular data. We are going to display and create metadata.
Scenario:
- We can get metadata simply by using info() command
- We can add metadata to the existing data and can view the metadata of the created data.
Steps:
- Create a data frame
- View the metadata which is already existing
- Create the metadata and view the metadata.
Here, we are going to create a data frame, and we can view and create metadata on the created data frame
View existing Metadata methods:
- dataframe_name.info() – It will return the data types null values and memory usage in tabular format
- dataframe_name.columns() – It will return an array which includes all the column names in the data frame
- dataframe_name.describe() – It will give the descriptive statistics of the given numeric data frame column like mean, median, standard deviation etc.
Create Metadata
We can create the metadata for the particular data frame using dataframe.scale() and dataframe.offset() methods. They are used to represent the metadata.
Syntax:
dataframe_name.scale=value
dataframe_name.offset=value
Below are some examples which depict how to add metadata to a DataFrame or Series:
Example 1
Initially create and display a dataframe.
Python3
import pandas as pd
data = { 'Name' : [ 'Sravan' , 'Deepak' , 'Radha' , 'Vani' ],
'College' : [ 'vignan' , 'vignan Lara' , 'vignan' , 'vignan' ],
'Department' : [ 'CSE' , 'IT' , 'IT' , 'CSE' ],
'Profession' : [ 'Student' , 'Assistant Professor' ,
'Programmer & ass. Proff' ,
'Programmer & Scholar' ],
'Age' : [ 22 , 32 , 45 , 37 ]
}
df = pd.DataFrame(data)
df
|
Output:

Then check dataframe attributes and description.
Python3
df.info()
df.columns
df.describe()
|
Output:

Initialize offset and scale of the dataframe.
Python3
df.scale = 0.1
df.offset = 15
print ( 'Scale:' , df.scale)
print ( 'Offset:' , df.offset)
|
Output:

We are storing data in hdf5 file format, and then we will display the dataframe along with its stored metadata.
Python3
storedata = pd.HDFStore( 'college_data.hdf5' )
storedata.put( 'data_01' , df)
metadata = { 'scale' : 0.1 , 'offset' : 15 }
storedata.get_storer( 'data_01' ).attrs.metadata = metadata
storedata.close()
with pd.HDFStore( 'college_data.hdf5' ) as storedata:
data = storedata[ 'data_01' ]
metadata = storedata.get_storer( 'data_01' ).attrs.metadata
print ( '\nDataframe:\n' , data)
print ( '\nStored Data:\n' , storedata)
print ( '\nMetadata:\n' , metadata)
|
Output:

Example 2
Series data structure in pandas will not support info and all methods. So we directly create metadata and display.
Python3
import pandas as pd
data = { 'Name' : [ 'Sravan' , 'Deepak' , 'Radha' , 'Vani' ],
'College' : [ 'vignan' , 'vignan Lara' , 'vignan' , 'vignan' ],
'Department' : [ 'CSE' , 'IT' , 'IT' , 'CSE' ],
'Profession' : [ 'Student' , 'Assistant Professor' ,
'Programmer & ass. Proff' ,
'Programmer & Scholar' ],
'Age' : [ 22 , 32 , 45 , 37 ]
}
ser = pd.Series(data)
ser
|
Output:

Now we will store the metadata and then display it.
Python3
storedata = pd.HDFStore( 'college_data.hdf5' )
storedata.put( 'data_01' , ser)
metadata = { 'scale' : 0.1 , 'offset' : 15 }
storedata.get_storer( 'data_01' ).attrs.metadata = metadata
storedata.close()
with pd.HDFStore( 'college_data.hdf5' ) as storedata:
data = storedata[ 'data_01' ]
metadata = storedata.get_storer( 'data_01' ).attrs.metadata
print ( '\nData:\n' , data)
print ( '\nStored Data:\n' , storedata)
print ( '\nMetadata:\n' , metadata)
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Aug, 2022
Like Article
Save Article