Python 从熊猫系列中筛选多个列

Python 从熊猫系列中筛选多个列,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据帧,为了创建一些分区,我必须从中检索唯一的值。我有这个部分,我可以得到一个小的数据帧,每一行都是一个特定的分区。我所面临的挑战是,然后我需要将原始数据帧过滤为仅适当的数据(而不修改原始帧以便过滤所有值),以便将其发送到S3 我无法根据小数据帧中的序列筛选数据帧 这是我的密码: df_partitions = df.groupby(['grid_id', 'case_id', 'snapshot_year', 'snapshot_month', 'snapshot_da

我有一个数据帧,为了创建一些分区,我必须从中检索唯一的值。我有这个部分,我可以得到一个小的数据帧,每一行都是一个特定的分区。我所面临的挑战是,然后我需要将原始数据帧过滤为仅适当的数据(而不修改原始帧以便过滤所有值),以便将其发送到S3

我无法根据小数据帧中的序列筛选数据帧

这是我的密码:

        df_partitions = df.groupby(['grid_id', 'case_id', 'snapshot_year', 'snapshot_month', 'snapshot_day']).size().reset_index()
        df_parts = df_partitions[['grid_id', 'case_id', 'snapshot_year', 'snapshot_month', 'snapshot_day']]

        for index, row in df_parts.iterrows() :
            dest_key_name = '/rec/{}/{}/{}/{}/{}/{}/{}'.format(row['grid_id'], row['case_id'],
                                                               row['snapshot_year'], row['snapshot_month'],
                                                               row['snapshot_day'], file_partition_time,
                                                               'df.csv')
            df_test = df
            filter_df = df_test[(df_test['grid_id'] == row['grid_id'] &
                                 df_test['case_id'] == row['case_id'] &
                                 df_test['snapshot_year'] == row['snapshot_year'] &
                                 df_test['snapshot_month'] == row['snapshot_month'] &
                                 df_test['snapshot_day'] == row['snapshot_day'])]
            print(filter_df)
以下是错误:

Traceback (most recent call last):
  File "<input>", line 8, in <module>
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/ops.py", line 954, in wrapper
    na_op(self.values, other),
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/ops.py", line 924, in na_op
    raise TypeError(msg)
TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]
KeyError: "['pjm' 'base' 2020 2 21] not in index"
Traceback (most recent call last):
  File "<input>", line 7, in <module>
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/frame.py", line 3164, in set_index
    frame.index = index
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/generic.py", line 3627, in __setattr__
    return object.__setattr__(self, name, value)
  File "pandas/_libs/properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/generic.py", line 559, in _set_axis
    self._data.set_axis(axis, labels)
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/internals.py", line 3074, in set_axis
    (old_len, new_len))
ValueError: Length mismatch: Expected axis has 130 elements, new values have 5 elements
以下是错误:

Traceback (most recent call last):
  File "<input>", line 8, in <module>
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/ops.py", line 954, in wrapper
    na_op(self.values, other),
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/ops.py", line 924, in na_op
    raise TypeError(msg)
TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]
KeyError: "['pjm' 'base' 2020 2 21] not in index"
Traceback (most recent call last):
  File "<input>", line 7, in <module>
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/frame.py", line 3164, in set_index
    frame.index = index
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/generic.py", line 3627, in __setattr__
    return object.__setattr__(self, name, value)
  File "pandas/_libs/properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/generic.py", line 559, in _set_axis
    self._data.set_axis(axis, labels)
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/internals.py", line 3074, in set_axis
    (old_len, new_len))
ValueError: Length mismatch: Expected axis has 130 elements, new values have 5 elements

以下是错误:

Traceback (most recent call last):
  File "<input>", line 8, in <module>
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/ops.py", line 954, in wrapper
    na_op(self.values, other),
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/ops.py", line 924, in na_op
    raise TypeError(msg)
TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]
KeyError: "['pjm' 'base' 2020 2 21] not in index"
Traceback (most recent call last):
  File "<input>", line 7, in <module>
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/frame.py", line 3164, in set_index
    frame.index = index
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/generic.py", line 3627, in __setattr__
    return object.__setattr__(self, name, value)
  File "pandas/_libs/properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/generic.py", line 559, in _set_axis
    self._data.set_axis(axis, labels)
  File "/local/workspace/FinBIPortal/env/RenewableEnergyValuationLambda-1.0/runtime/lib/python3.6/site-packages/pandas/core/internals.py", line 3074, in set_axis
    (old_len, new_len))
ValueError: Length mismatch: Expected axis has 130 elements, new values have 5 elements
回溯(最近一次呼叫最后一次):
文件“”,第7行,在
文件“/local/workspace/FinBIPortal/env/renewablenergyvaluationlambda-1.0/runtime/lib/python3.6/site packages/pandas/core/frame.py”,第3164行,在集合索引中
frame.index=索引
文件“/local/workspace/FinBIPortal/env/renewablenergyvaluationlambda-1.0/runtime/lib/python3.6/site packages/pandas/core/generic.py”,第3627行,位于__
返回对象。\uuuu setattr\uuuuu(self、name、value)
文件“pandas/_libs/properties.pyx”,第69行,在pandas._libs.properties.AxisProperty.\uu\set中__
文件“/local/workspace/FinBIPortal/env/renewablenergyvaluationlambda-1.0/runtime/lib/python3.6/site packages/pandas/core/generic.py”,第559行,在集合轴中
自身数据。设置轴(轴、标签)
文件“/local/workspace/FinBIPortal/env/renewablenergyvaluationlambda-1.0/runtime/lib/python3.6/site packages/pandas/core/internals.py”,第3074行,在集合轴中
(旧的、新的)
ValueError:长度不匹配:预期轴有130个元素,新值有5个元素

这里有一个非常简单的解决方案。当您尝试
df[(…)&(…)]
时,根据多个条件进行筛选的格式为
df[(…)&(…)]
。将要设置的
filter\u df
的括号括起来