Pandas DataFrame ValueError:传递的项目数错误 2,位置暗示 1

Posted

技术标签:

【中文标题】Pandas DataFrame ValueError:传递的项目数错误 2,位置暗示 1【英文标题】:Pandas DataFrame ValueError: Wrong number of items passed 2, placement implies 1 【发布时间】:2021-08-10 16:21:36 【问题描述】:

代码如下:

a = 
    "Date": ["2020-01-01", "2020-01-01", "2020-01-01", "2020-01-01", "2020-01-02", "2020-01-02", "2020-01-02", "2020-01-02", "2020-01-03", "2020-01-03", "2020-01-03", "2020-01-03"],
    "Count": [39, 43, 30, 27, 6, 7, 8, 3, 11, 16, 15, 19]


b = 
    "Date_used": ["2020-01-01", "2020-01-02", "2020-01-02", "2020-01-03"],
    "Value": [39, 7, 5, 12]


df = pd.DataFrame(a)
df["Date"] = pd.to_datetime(df["Date"])

df2 = pd.DataFrame(b)
df2["Date_used"] = pd.to_datetime(df2["Date_used"])

df2["In df?"] = df[["Date", "Count"]].isin(df2[["Date_used","Value"]])

我正在尝试在 df2 中创建一个列,它将在 df 中查找与其相应行匹配的行,如果找到,则返回 True,否则返回 False。 df2 的理想结果如下:

Date Value In df?
2020-01-01 39 True
2020-01-02 7 True
2020-01-02 5 False
2020-01-03 12 False

如您所见,df2 中的前两行在 df 中匹配,但后两行不匹配。

我收到以下错误:

Traceback (most recent call last):
  File ".../b.py", line 22, in <module>
    df2["In df?"] = df[["Date", "Count"]].isin(df2[["Date_used","Value"]])
  File "...\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 3163, in __setitem__
    self._set_item(key, value)
  File "...\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 3243, in _set_item
    NDFrame._set_item(self, key, value)
  File "C:\Users\Ruairidh Livingstone\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 3829, in _set_item
    self._mgr.insert(len(self._info_axis), key, value)
  File "...\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\managers.py", line 1203, in insert
    block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc + 1))
  File "...\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\blocks.py", line 2732, in make_block
    return klass(values, ndim=ndim, placement=placement)
  File "...\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\blocks.py", line 142, in __init__
    raise ValueError(
ValueError: Wrong number of items passed 2, placement implies 1

我认为部分问题是 dfdf2 的长度不同但是我不确定如何解决这个问题,任何帮助将不胜感激.

提前致谢!

【问题讨论】:

您共享的代码不起作用,您正在使用df2 中的"Date_used" 列,但该列不存在(尽管您有一个名为"Date" 的列)。也许您错误地复制了代码?还请包括完整的错误回溯 @MatteoZanoni 是的,我的错,现在应该修复 【参考方案1】:

一个选项通过merge:

df2 = df2.merge(df, left_on=df2.columns.to_list(), right_on=df.columns.to_list(), how='left', indicator='In_df').drop(
    df.columns.to_list(), 1).assign(In_df=lambda x: x['In_df'].map('both': True, 'left_only': False))

输出:

   Date_used  Value  In_df
0 2020-01-01     39   True
1 2020-01-02      7   True
2 2020-01-02      5  False
3 2020-01-03     12  False

【讨论】:

【参考方案2】:

这样使用pd.merge

df2['In df?'] = df2.merge(df.drop_duplicates(), left_on=['Date','Value'], \
                    right_on=['Date','Count'], how='left', indicator=True)

df2['In df?'] = np.where(df_all['_merge']=='both', True, False)

或带地图

df2['In df?'] = df2.merge(df.drop_duplicates(), left_on=['Date','Value'],\
                          right_on=['Date','Count'], how='left', indicator=True)\
                         .map('both': True, 'left_only': False)

输出

    Date         Value  In df?
0   2020-01-01   39     True
1   2020-01-02   7      True
2   2020-01-02   5      False
3   2020-01-03   12     False

【讨论】:

以上是关于Pandas DataFrame ValueError:传递的项目数错误 2,位置暗示 1的主要内容,如果未能解决你的问题,请参考以下文章

pandas.DataFrame.loc好慢,怎么遍历访问DataFrame比较快

将 Pandas Multiindexed DataFrame 与 Singleindexed Pandas DataFrame 合并

pandas.DataFrame.loc好慢,怎么遍历访问DataFrame比较快

python pandas dataframe 写入hdfs

pandas怎么选取dataframe中几列

详解pandas获取Dataframe元素值的几种方法