Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe having an additional "layer"

Tags:

python

pandas

Suppose you have the following dataframe:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.nan,columns=['A','B','C'],index=[0,1,2])

Suppose I want an additional "layer" on top of this pandas dataframe, such that column A, row 0 would have its value, column B, row 0 would have a different value, column C row 0 would have something, column A row 1 and so on. So like a dataframe on top of this existing one.

Is it possible to add other layers? How does one access these layers? Is this efficient, i.e. should I just use a separate data frame all together? And one would save these multiple layers as a csv, by accessing the individual layers?, or is there a function that would break them down into different worksheets in the same workbook?

like image 520
Naz Avatar asked Mar 06 '17 18:03

Naz


People also ask

Is there a 4-Dimensional data structure in pandas?

No, there is no 4-dimensional data structure in pandas. All you get is 3. But I suspect you only need three. Adding several "layers" doesn't add a dimension, just another layer. @StevenRumbalski Ah!

How to convert a Dataframe to feature?

You can probably use memory layer. Just convert each line of your dataframe to feature.

Do you have geometries in The Dataframe or is it only table?

Do you have geometries in the dataframe or is it only a table? It is only a table. Can you show us what your dataframe looks like? You can probably use memory layer. Just convert each line of your dataframe to feature.


1 Answers

pandas.DataFrame cannot have 3 dimensions:

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.

However, there is a way to fake 3-dimensions with MultiIndex / Advanced Indexing:

Hierarchical indexing (MultiIndex)

Hierarchical / Multi-level indexing is very exciting as it opens the door to some quite sophisticated data analysis and manipulation, especially for working with higher dimensional data. In essence, it enables you to store and manipulate data with an arbitrary number of dimensions in lower dimensional data structures like Series (1d) and DataFrame (2d).

If you really need that extra dimension go with pandas.Panel:

Panel is a somewhat less-used, but still important container for 3-dimensional data.

but don't miss this important disclaimer from the docs:

Note: Unfortunately Panel, being less commonly used than Series and DataFrame, has been slightly neglected feature-wise. A number of methods and options available in DataFrame are not available in Panel.

There is also pandas.Panel4D (experimental) in the unlikely chance that you need it.

like image 157
Steven Rumbalski Avatar answered Sep 20 '22 18:09

Steven Rumbalski