熊猫分组或不分组的条件

时间:2019-08-30 14:40:02

标签: python python-3.x pandas group-by

如果要根据用户输入做出某种选择,我将尝试创建“条件分组依据”。如果数据框中存在'ZONE'列,我想按该区域分组,然后遍历功能列表 ['Var1','Var2'] 。< / p>

除了没有'ZONE'列外,我只想遍历功能列表而不使用groupby。

我的伪代码示例是:

import pandas as pd

data = pd.DataFrame({'County' : [1, 2, 2, 2, 3, 3], 'ZONE' : [88, 88, 19, 19, 10, 19], 'Var1' : [78, 90, 97, 100, 12, 140], 'Var2' : [56, 92, 122, 134, 120, 140]})

features = ['Var1', 'Var2']

if 'ZONE' in data.columns:
    data_grouped = data.groupby(['ZONE'])
if 'ZONE' not in data.columns:
    data_grouped = data.copy()

# iterate over grouped zone data
for zone, zone_data in data_grouped:
# iterate over feature columns
      for feature in features:
          data_feature = data_grouped[feature]
          print(data_feature)
          ......make graphs and other things with this grouped data.....

上面的代码适用于groupby ZONE情况,但是如果没有ZONE,我不知道如何忽略此groupby并仅对单个for循环中的功能进行迭代-我想拥有一个for循环而不是同时打破这两种情况并重复一堆图形代码。

有没有办法做到这一点?也许是itertools解决方案?

1 个答案:

答案 0 :(得分:0)

我不确定您想要什么最终结果。我认为有更好的方法可以不循环地获得最终结果。

无论如何,有一种简单的方法可以处理所需的内容:

if 'ZONE' in data.columns:
    data_grouped = data.groupby(['ZONE'])
if 'ZONE' not in data.columns:
    data_grouped = ['NoZone', data]

for zone, zone_data in data_grouped:
# iterate over feature columns
      for feature in features:
          data_feature = zone_data[feature]
          print(data_feature)
          ......make graphs and other things with this grouped data.....