Missleading display of values in pandas column when each cell is a list

Question:

In jupiter notebook

This column made up of two different lists, are displayed the same way.

pd.DataFrame({
    "user": ["a", "b"], 
    "values": [['1', '2, 1', '2', '3'], ['1, 2', '1, 2, 3']]
              })

![enter image description here

Q: how to display these lists so that they look different.

Asked By: Leo

||

Answers:

I am not sure what is the exact question, but yes there is ambiguity.

You also have ambiguity with other data types. For example tuples and frozensets look alike:

pd.Series([('a', 'b'), frozenset(['a', 'b'])])

0    (a, b)
1    (a, b)
dtype: object

One thing you could do would be to use the repr of your objects, which doesn’t fully avoid ambiguity but makes things a bit more explicit for strings and tuples/frozensets:

df = pd.DataFrame({
    'A': ['a, b', 'b, c, d', 'c'], 
    'B': [1, 2, 3]
})

df.applymap(repr)

           A  B
0     'a, b'  1
1  'b, c, d'  2
2        'c'  3

pd.Series([('a', 'b'), frozenset(['a', 'b'])]).apply(repr)

0               ('a', 'b')
1    frozenset({'a', 'b'})
dtype: object

Note that using lists as items is usually not a good practice in pandas (this doesn’t allow vectorial functions).

Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.