Question: I have one array that I am trying to combine all the sub values that have the same key name and add an extra if there is only 2 with the same key name.

her is a saml example of the array this array hase 92 rows

```
Array
(
    [0] => Array ( 
            [N_127y4Gral] => Array ( 
                    [0] => 302
                )
        )
    [1] => Array ( 
            [N_127y4Gral] => Array ( 
                    [0] => 288
                )
        )
    [2] => Array ( 
            [N_144y4Gral] => Array ( 
                    [0] => 317
                )
        )
    [3] => Array ( 
            [N_144y4Gral] => Array ( 
                    [0] => 307
                )
        )
    [4] => Array ( 
            [N_144y4Gral] => Array ( 
                    [0] => 332
                )
        )
)

```

and this is the output I am looking for.

```
Array
(
    [0] => Array ( 
            [N_127y4Gral] => Array ( 
                    [0] => 302
                    [1] => 288
                    [2] => 0
                )
        )
    [1] => Array ( 
            [N_144y4Gral] => Array ( 
                    [0] => 317
                    [1] => 307
                    [2] => 332
                )
        )
)

```

I think i need to use a foreach or for loop to make this happen but i'm not sure on how to structure the function, any suggestion is greatly appreciated.

Answer: 
If the column is a string column just filter values by string `contains`. Assuming your df is `group`. Since you want all the results to show, you could make this not-elegant-solution that makes 3 dataframes:

**first, the ones that contains CONTRATO:**

```
df1 = group.loc[group['VALOR_NEW'].fillna('nada').str.contains('CONTRATO')].head(3)

```

**Then another that doesnt (note the "~" at the start of the .loc, it means opposite:**

```
df2 = group.loc[~group['VALOR_NEW'].fillna('nada').str.contains('CONTRATO')]

```

**Finally you concat both with the ones with CONTRATO first:**

```
df3 = pd.concat([df1,df2])

```

Note that it's case sensitive and that I added a `fillna()` because you can't search if it has nan values.

Edit: added the `.head(3)` at the end to just get the first 3 rows, as you requested.

edit2: edited the original solution

vlwflws
IIUC, you need to check your grouped dataframes for `'Contrato'` in the `'VALOR_NEW'` column within the first three rows. This check should do it:

```
if (group['VALOR_NEW'].head(3) == 'Contrato').any():
    print(group)

```