0

I used to run the following operation using pandas version 0.23.4. Now using pandas 1.3.3 (with Python 3.7) the last line raises "TypeError: Cannot broadcast np.ndarray with operand of type <class 'list'>". Any ideas how to accomplish such operations in the more recent pandas versions? Thanks in advance.

d = {'col1': [1, 2, 3], 'col2': [3, 4, 4]}
df = pd.DataFrame(data=d)
df["example" ] = np.empty((len(df), 0)).tolist()
df.loc[df.col2 == 4, "example"] += [str("A")] # This does no longer work in pandas 1.3.3

This question is related to this post, where however the answer to my question is missing.

1 Answers1

0

You can use apply() instead:

import pandas as pd
import numpy as np
d = {'col1': [1, 2, 3], 'col2': [3, 4, 4]}
df = pd.DataFrame(data=d)
df["example" ] = np.empty((len(df), 0)).tolist()
df.loc[df.col2 == 4, "example"].apply(lambda x: x.append(str("A")))
print(df)

Output:

    col1  col2 example
 0     1     3      []
 1     2     4     [A]
 2     3     4     [A]
  • Thanks a lot, this works, although I hoped for a solution without lambda function . – cbra0410 Feb 15 '22 at 13:13
  • I dont know if it is possible. Looks like the np.arrays does not have the same methos of a standard list. Take a look in this topic: [link](https://stackoverflow.com/questions/15801707/broadcasting-function-calls-in-np-array) – Marcelo Ferraz Feb 15 '22 at 14:26