简体   繁体   中英

How to set masked pandas DataFrame column to column of lists

How can I set certain cells in a column to a list from a list of lists where the list of lists has the same length as the number of cells?

When running the portion I tried, I get the following error:

ValueError: Must have equal len keys and value when setting with an ndarray

My desired DataFrame, as defined explicitly below with desired looks like this:

   include   array
0     True  [1, 2]
1    False     NaN
2    False     NaN
3     True  [3, 4]
4    False     NaN

Code tried:

import pandas as pd

# This is what I tried
a = pd.DataFrame({'include': [True, False, False, True, False]})
a.loc[a['include'], 'array'] = [[1, 2], [3, 4]]

# This is what I want
desired = pd.DataFrame({'include': [True, False, False, True, False],
                        'array': [[1, 2], np.nan, np.nan, [3, 4], np.nan]})

Not exactly sure what you want to do, but you can cast it to a pandas.Series and allign the indices:

a.loc[a['include'], 'array'] = pd.Series([[1, 2], [3, 4]], index=[0, 3])

print(a)
   include   array
0     True  [1, 2]
1    False     NaN
2    False     NaN
3     True  [3, 4]
4    False     NaN

To keep the assign of the index general and not hard coded, use:

a.loc[a['include'], 'array'] = pd.Series([[1, 2], [3, 4]], a[a['include']].index)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM