Skip to content

BUG: One item lists are assigned into dataframes as single items #16864

Open
@elielhojman

Description

@elielhojman

Code Sample, a copy-pastable example if possible

df = pd.DataFrame(data={'A':[1, 2, 3, 4], 'B':[5, 6, 7, 8], 'C':[8, 9, 10, 13]})
df['new_col'] = np.nan
df.loc[[0,1], 'new_col'] = [['one_item'], ['one_item']]
df.loc[[2,3], 'new_col'] = [['one_item'], ['two', 'items']]
print(df)
#     A  B   C         new_col
# 0  1  5   8           one_item
# 1  2  6   9           one_item
# 2  3  7  10          [one_item]
# 3  4  8  13          [two, items]

Problem description

I would like to create a new column and load the values into it gradually, however in the first assignment to the 'new_col' as the list has only one item they are assigned as items and not as lists.

Expected Output

#    A  B   C         new_col
# 0  1  5   8          [one_item]
# 1  2  6   9          [one_item]
# 2  3  7  10          [one_item]
# 3  4  8  13          [two, items]

Output of pd.show_versions()

INSTALLED_VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.20.2
pytest: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.13.0
scipy: 0.19.1
xarray: None
IPython: 5.4.1
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions