0

Hiii, my code is here:

def get_sway(vote):
  if vote == 0:
    return df['selfLR'] - df['ClinLR']
  else:
    return df['selfLR'] - df['DoleLR']

df['sway_diff'] = df.vote.apply(get_sway)
df

Error is here:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-9003eee7857b> in <module>
      6     return df['selfLR'] - df['DoleLR']
      7 
----> 8 df['sway_diff'] = df.vote.apply(get_sway)
      9 df
     10 

1 frames
/usr/local/lib/python3.8/dist-packages/pandas/core/frame.py in _set_item_frame_value(self, key, value)
   3727             len_cols = 1 if is_scalar(cols) else len(cols)
   3728             if len_cols != len(value.columns):
-> 3729                 raise ValueError("Columns must be same length as key")
   3730 
   3731             # align right-hand-side columns if self.columns

ValueError: Columns must be same length as key

I don't know why the number of columns are not matched. They uses same DataFrame. T-T I think this is actually not a pandas series problem... Why this error occurred? Thanks a lot.

0 Answers0