Home > Net >  "ValueError: Columns must be same length as key" when filtering dataframe with isin(list)
"ValueError: Columns must be same length as key" when filtering dataframe with isin(list)

Time:11-20

I am trying to filter a column in my dataframe based on values from a list, here is the snippet of my code where it's going wrong (replaced values for simplicity's sake)

import pandas as pd
from pandas import Series

df['Campaign']=df['Location']
campaign_list = ['a', 'b']
df['Campaign']=df[df['Campaign'].isin(campaign_list)]

here is an example of what the dataframe looks like before the problem code

Location    Billed Amount   TransactionID   Campaign
a           Na              x               a
b           Na              y               b
c           Na              z               c
d           Na              xx              d
e           Na              xy              e
f           Na              xz              f

here is what my desired df should look like

Location    Billed Amount   TransactionID   Campaign
a           NaN             x               a
b           NaN             y               b
c           NaN             z               NaN
d           NaN             xx              NaN
e           NaN             xy              NaN
f           NaN             xz              NaN

Here is the error I am receiving, which is strange because I ran this exact code yesterday and didn't have any issue. Is there something obvious here I'm just not seeing?

~\anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
   3600             self._setitem_array(key, value)
   3601         elif isinstance(value, DataFrame):
-> 3602             self._set_item_frame_value(key, value)
   3603         elif (
   3604             is_list_like(value)

~\anaconda3\lib\site-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

CodePudding user response:

Use Series.where

df['Campaign'] = df['Campaign'].where(lambda camp: camp.isin(campaign_list))

or

df['Campaign'] = df['Campaign'].where(df['Campaign'].isin(campaign_list))

Output:

>>> df 

   Location Billed Amount TransactionID Campaign
0         0            Na             x        a
1         1            Na             y        b
2         2            Na             z      NaN
3         3            Na            xx      NaN
4         4            Na            xy      NaN
5         5            Na            xz      NaN

CodePudding user response:

This should work:

df=df[df['Campaign'].isin(campaign_list)]
  • Related