python 3.x – Iterate through groups of rows with different indexed values

The data looks like this:

Data = {& # 39; group_id: [‘1′,’1′,’1′,’1′,’2′,’2′,’2’],
& # 39; fountain & # 39; [‘Twitter’, ‘Instagram’, ‘Twitter’, ‘Facebook’, ‘Facebook’, ‘Twitter’, ‘Instagram’, ‘Facebook’]
& # 39; Gravity & # 39; [4,2,7,4,8,9,3,5]}

I need:

1) Take the first row of the Severity Code of each group
2) Obtain the absolute value of all the rows (difference) of the identified severity code of each group (from # 1). Example: Group severity code 1 (4) … first row diff = 0; second row diff = 2; third row diff = 3; The same for group 2.
3) In each group, find the closest neighbor of each source, up to the severity of the first row.

I have identified the first row and indexed the severity code. When iterating, the code only uses the last indexed severity code to calculate the difference.

df = pd.DataFrame (Data)
first_row = b.groupby (['group_id']).First()
for the row in first1.itertuples (index = True, name = & # 39; Pandas & # 39;):
value = getattr (row, & # 39; Severity & # 39;)
df['dif'] = (df['Severity'] - value) .abs ()

I hope the output is in a Dataframe with a column & # 39; dif & # 39; added I can extract nearest neighbors in each group for each source where True. Repeat the process: extract the rows where True and pass the False to find additional rows with a new severity of the first row. Repeat again until there are no rows, or all rows are False.