"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)
我正在尝试根据列表中的值过滤我的数据框中的列,这是我的代码片段出错的地方(为简单起见替换值)
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
这是我收到的错误,它是 st运行ge,因为我昨天 运行 这个确切的代码并且没有任何问题。这里有什么明显的东西我只是没看到吗?
~\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
这应该有效:
df=df[df['Campaign'].isin(campaign_list)]
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
我正在尝试根据列表中的值过滤我的数据框中的列,这是我的代码片段出错的地方(为简单起见替换值)
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
这是我收到的错误,它是 st运行ge,因为我昨天 运行 这个确切的代码并且没有任何问题。这里有什么明显的东西我只是没看到吗?
~\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
这应该有效:
df=df[df['Campaign'].isin(campaign_list)]
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