I have a dataframe
import pandas as pd
df = pd.DataFrame({
'ID': [1, 1, 1, 2, 2, 2],
'value': [100, 120, 130, 200, 190, 210],
'value2': [2100, 2120, 2130, 2200, 2190, 2210],
'state': ['init','mid', 'final', 'init', 'mid', 'final'],
})
I want to add another row, which is the difference of 'final' and 'init' in the state column for each ID.
The result should look like
import pandas as pd
df = pd.DataFrame({
'ID': [1, 1, 1, 2, 2, 2, 1, 2],
'value': [100, 120, 130, 200, 190, 210, 30, 10],
'value2': [2100, 2120, 2130, 2200, 2190, 2210, 100, 10],
'state': ['init','mid', 'final', 'init', 'mid', 'final', 'diff', 'diff'],
})
I have tried the following. But I get AttributeError: 'function' object has no attribute 'groupby' error
df1 = df.copy()
df1 = df[df.state.isin(['final', 'init'])].copy
s = df1.groupby('ID', sort=False).diff().dropna.assign(id=df1['ID'].unique(), state='diff')
df = df.append(s, sort=True).sort_values("ID")