"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Insert Lists into Pandas DataFrame Cells Without Errors?

How to Insert Lists into Pandas DataFrame Cells Without Errors?

Published on 2024-11-06
Browse:634

How to Insert Lists into Pandas DataFrame Cells Without Errors?

Inserting Lists into Pandas Cells

Problem

In Python, attempting to insert a list into a cell of a Pandas DataFrame can result in errors or unexpected results. For example, when trying to insert a list into cell 1B of a DataFrame df:

df = pd.DataFrame({'A': [12, 23], 'B': [np.nan, np.nan]})
abc = ['foo', 'bar']

The following attempts to insert the abc list into 1B, but they produce errors or incorrect insertion:

  1. df.ix[1,'B'] = abc - Error: Must have equal len keys and value when setting with an iterable
  2. df.ix[1,'B'] = [abc] - Inserts a list with one element: [['foo', 'bar']]
  3. df.ix[1,'B'] = ', '.join(abc) - Inserts a string: "foo, bar"
  4. df.ix[1,'B'] = [', '.join(abc)] - Inserts a list with one element: ['foo, bar']

Solution

To insert lists into cells of a DataFrame without errors, use the at method, which always refers to a single value:

df.at[1, 'B'] = ['foo', 'bar']

This will insert the abc list into 1B as expected:

    A  B
0  12  NaN
1  23  ['foo', 'bar']

Note that the DataFrame column must have dtype=object to allow list insertion. For example:

df['B'] = df['B'].astype('object')
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3