[Solved] Python – How to Compare a column value of one row with value in next row


Use groupby (http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html)

Assume your input is saved in a pandas Dataframe (or equivalently save it into csv and read it using pandas.read_csv).
Now you can loop over the groups with same S.No values with the following:

output = {}

for key, group in df.groupby('S.No.'):
#   print key
#   print group

   output[key] = {}
   output[key]['Details'] = group['Details'].values.tolist()
   output[key]['Date Time Diff'] = pd.to_datetime(group['Datetime']).diff().iloc[-1]

Output of the above

1 {'Date Time Diff': Timedelta('0 days 08:59:00'), 'Details': ['asd', 'dfg']}
2 {'Date Time Diff': Timedelta('1 days 00:00:00'), 'Details': ['dfg', 'gfd', 'gfd']}
3 {'Date Time Diff': Timedelta('1 days 00:00:00'), 'Details': ['gfd', 'abc']}
4 {'Date Time Diff': Timedelta('1 days 00:00:00'), 'Details': ['abc', 'def']}

3

solved Python – How to Compare a column value of one row with value in next row