1

我正在尝试根据列表中的值过滤数据框中的列,这是我的代码片段出错的地方(为简单起见,替换了值)

import pandas as pd
from pandas import Series

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

这是问题代码之前数据框的示例

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

这是我想要的 df 应该是什么样子

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

这是我收到的错误,这很奇怪,因为我昨天运行了这个确切的代码并且没有任何问题。这里有什么明显的东西我没看到吗?

~\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
4

2 回答 2

1

采用Series.where

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

或者

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

输出:

>>> 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
于 2021-11-19T15:58:16.867 回答
-1

这应该有效:

df=df[df['Campaign'].isin(campaign_list)]
于 2021-11-19T15:53:34.703 回答