Is there a way to store two numbers into a pandas cell?

I have a pandas dataframe

df = pd.DataFrame([[0, 1], [2, 3]],
                                    index=['cat', 'dog'],
                                    columns=['weight', 'height'])

but when I try to store two numbers (whether it’s a tuple or a list) into a cell, an error message emerges

df.loc['dog', 'height'] = (1,2)   # error!!

Is there a way to put two numbers into a cell?

First off, I’d create the df like this:

data = {
    'weight': [0, 2],
    'height': [1, 3]

labels = ['cat', 'dog']

# create DataFrame
df = pd.DataFrame(data=data, index=labels)

… for no other reason than ‘human readability’.

You want two numbers in a data field? I can’t think why, but you can use a tuple or a list: this is the tuple way:

data = {
    'weight': [0, 2],
    'height': [1, (3, 4)]

To my mind, that’s akin to having two numbers in a spreadsheet cell, but I’m in no way a experienced pandas user, so maybe there’s a good reason that I’m unaware of, for doing that in a pandas df.

You can use multi-level indexing to store multiple numbers within a cell. This involves creating a hierarchical index with multiple levels. However, this approach may complicate data manipulation and analysis. Here’s an example:

import pandas as pd

data = {‘Numbers’: [(1, 2), (3, 4)]}
df = pd.DataFrame(data)

df = df.set_index([[0, 1], [‘A’, ‘B’]])



0 A (1, 2)
1 B (3, 4)

Pandas is intended for number-crunching, not to represent complex data structures. A big part of the point of having a DataFrame is knowing that all the values in a column have the same type - and normally it will be a simple type that supports efficient operations that can be optimized behind the scenes (things like numeric types, dates, and strings used in relatively simple ways). Another big part is that the data makes sense according to the labels. What would it mean for a dog to have a height of (1, 2)? And why would it make sense for a dog’s height to be a different type than a cat’s?

What underlying problem are you really trying to solve - why do you want Pandas to work like this?