I have a dataframe
import pandas as pd
df = pd.DataFrame([[2, 1,5], [8, 3,4]],
columns=['weight', 'height', 'blood_pressure'])
This is how I flatten it to a single column. I wonder whether there is a better way
id1 = pd.MultiIndex.from_product([df.index, df.columns], names=('animal', 'measure'))
df2 = pd.DataFrame(np.array(df).reshape(len(df)*len(df.columns),1), index = id1)
Is there any way to get a series from the single dataframe column? Thanks!
A dataframe column is a pd.Series. So, I don’t quite get your question. What is it you want to do (what’s the purpose of the flattening/multi-index creation)?
I want to represent a 3-dimensional dataset by a typical (2-dimensional) dataframe. You are right, a single dataframe column would work. Thank you so much Hans!
So, the rows are also pd.Series objects, so you could also consider just doing
df.transpose()) to convert the original df:
weight 2 8
height 1 3
blood_pressure 5 4
It all depends on what kind of further processing you want to do…
Just to clarify, the problem I have is that I have zoo 1, zoo 2, zoo 3, … For each zoo, I have a cat and a dog, and I have their data (weight, height, blood_pressure). Since it’s hard to visualize the 3-dimensional data, I try to convert the dataframe for each zoo to a column, so that I may show all data in a typical 2-dimensional dataframe.