Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding meta-information/metadata to pandas DataFrame

Tags:

python

pandas

Is it possible to add some meta-information/metadata to a pandas DataFrame?

For example, the instrument's name used to measure the data, the instrument responsible, etc.

One workaround would be to create a column with that information, but it seems wasteful to store a single piece of information in every row!

like image 681
P3trus Avatar asked Feb 04 '13 13:02

P3trus


People also ask

How do I get pandas metadata?

We can get metadata simply by using info() command.

Can pandas read .data file?

pandas provides the read_csv() function to read data stored as a csv file into a pandas DataFrame . pandas supports many different file formats or data sources out of the box (csv, excel, sql, json, parquet, …), each of them with the prefix read_* .

How do you add a label in pandas?

set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one. so df2.index.name = 'x label' would work too. set_xlabel or set_ylabel are not working for pandas 0.25.


1 Answers

Sure, like most Python objects, you can attach new attributes to a pandas.DataFrame:

import pandas as pd df = pd.DataFrame([]) df.instrument_name = 'Binky' 

Note, however, that while you can attach attributes to a DataFrame, operations performed on the DataFrame (such as groupby, pivot, join or loc to name just a few) may return a new DataFrame without the metadata attached. Pandas does not yet have a robust method of propagating metadata attached to DataFrames.

Preserving the metadata in a file is possible. You can find an example of how to store metadata in an HDF5 file here.

like image 142
unutbu Avatar answered Oct 16 '22 17:10

unutbu