0

I am trying to populate a column (dtype object) of my dataframe. What I found strange was that I can not do it with .at but it raises **** ValueError: Incompatible indexer with Series* with .loc. Here is the minimal reproducible example of this behaviour:

import pandas as pd
import numpy as np

df = pd.DataFrame(data={'uv': [1., 2.], 'vis': [3., 4.]})
df["Values"]= np.empty((len(df)),dtype=object)

df["Values"].at[0] = {'a': [0., 1.], 'vis': [5., 6.]}  #works
df.loc[1, "Values"] = {'a': [0., 1.], 'vis': [5., 6.]}  #does not work

Please not that this behaviour is only limited with dtype object, but works fine with floats, integers etc.

Vinod Kumar
  • 1,043
  • 1
  • 11
  • 22
  • 1
    this is because you have a 2D vs 1D structure. `at` is designed to work with single items while `loc` can broadcast. You can combine both row/col in a single `at`: `df.at[1, 'Values'] = {'a': [0., 1.], 'vis': [5., 6.]}`. `loc` works with a float as there is not possible expansion of a float (it is not iterable). – mozway May 10 '22 at 09:44

0 Answers0