Python Pandas Insert List into a Cell

Python pandas insert list into a cell

df3.set_value(1, 'B', abc) works for any dataframe. Take care of the data type of column 'B'. For example, a list can not be inserted into a float column, at that case df['B'] = df['B'].astype(object) can help.

Insert list in pandas dataframe cell

Use pd.Series inside constructor, since dict values sizes are not equal, then set_axis to add column names i.e

mapping_dict = {'A':['a', 'b', 'c', 'd'], 'B':['aa', 'bb', 'cc']}

df = pd.DataFrame(pd.Series(mapping_dict).reset_index()).set_axis(['Key','Value'],1,inplace=False)

Key Value
0 A [a, b, c, d]
1 B [aa, bb, cc]

Option 2 , convert the dict items to list then pass it to constructor:

df = pd.DataFrame(list(mapping_dict.items()),columns=['Key','Value'])

Python pandas insert list into a cell

df3.set_value(1, 'B', abc) works for any dataframe. Take care of the data type of column 'B'. For example, a list can not be inserted into a float column, at that case df['B'] = df['B'].astype(object) can help.

Insert list into cells which meet column conditions

Another solution is create Series filled by list with shape for generating length of df:

df.loc[(df['A'] > 1) & (df['B'] > 1), "D"] = pd.Series([[1,0]]*df.shape[0])
df.loc[(df['A'] == 1) , "D"] = pd.Series([[0,2]]*df.shape[0])
df.loc[(df['A'] == 2) & (df['C'] != 0) , "D"] = pd.Series([[2,0]]*df.shape[0])
print (df)
A B C D
0 3 2 1 [1, 0]
1 4 2 3 [1, 0]
2 1 4 1 [0, 2]
3 2 2 3 [2, 0]

Append list into a single pandas data frame cell

Initialize your DataFrame with a placeholder for your list, then assign via loc:

df = pd.DataFrame(index=[0], columns=['A'])
df
A
0 NaN

df.loc[0, 'A'] = parameters
df
A
0 [1, 3, 4, 5]

Insert a list of containing one element into a pandas dataframe cell

You may use df.apply to include those elements in a list after you assigned that item:

df.loc[df['Power']==73, 'Duration'] = [1]
df.Duration.apply(lambda x: [x] if type(x) is not list else x)

How can I insert a column of a dataframe in pandas as a list of a cell into another dataframe?

you can select the rows withquery, then groupby+agg:

(df.query('Type') # or 'Type == "True"' if strings
.groupby('Category', as_index=False)
.agg({'Data': list, 'Type': 'first'})
)

output:

  Category                          Data  Type
0 A [30275, 35886, 35883] True
1 C [28129, 39900, 28129, 31571] True
2 D [30351, 35856] True

Inserting list into a cell - why does loc ACTUALLY work here?

This occurs because loc does a bunch of checking for all the many usecases which it supports. (Note: The history was that loc and iloc were created to remove ambiguity of ix, way back in 2013 v0.11, but even today there's still a lot of ambiguity in loc.)

In this case df.loc[1, 'B'] can either return:

  • a single element (as in this case, when there is a unique index/column for 1/'B').
  • a Series (if EITHER one of the 1/'B' appears in index/columns multiple times).
  • a DataFrame (if BOTH 1/'B' appear in index/columns multiple times).

Aside: iloc suffers the same issue in this case, even though it's always going to be the first case, but that may be because loc and iloc share this assignment code.

So that pandas needs to support all of those cases for assignment!

An early part of the assignment logic converts the list (of lists) into a numpy array:

In [11]: np.array(['m', 'n', 'o', 'p']).shape
Out[11]: (4,)

In [12]: np.array([['m', 'n', 'o', 'p']]).shape
Out[12]: (1, 4)

So you can't just pass the list of lists and expect to get the right array. Instead you could to explictly set into an object array:

In [13]: a = np.empty(1, dtype=object)

In [14]: a[0] = ['m', 'n', 'o', 'p']

In [15]: a
Out[15]: array([list(['m', 'n', 'o', 'p'])], dtype=object)

Now you can use this in the assignment:

In [16]: df.loc[0, 'B'] = a

In [17]: df
Out[17]:
A B
0 12 [m, n, o, p]
1 23 [c, d]

It's still not ideal, but to reiterate there are sooo many edge cases in loc and iloc, that the solution is to be as explicit as possible to avoid them (use at here). And more generally, as you know, avoid using lists inside a DataFrame!



Related Topics



Leave a reply



Submit