Missleading display of values in pandas column when each cell is a list
Question:
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).
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).