Language: Python 3 Platform : Jupyter Notebook
I want to count the amount of messages sent by the same user at the same date and time in a dataframe. I tried to do it using a recursive function, following this example.
Define recursive function in Pandas dataframe
below is my attempt.
interaction = 0
for i, row in df.iterrows():
if ((df2['names'].iloc[i] == (df2['names'].iloc[i-1]) & (df2['time'].iloc[i] == df2['time'].iloc[i-1]) & (df2['date'].iloc[i]== df2['date'].iloc[i-1]))
interaction = interaction
else:
interaction = interaction+1
return interaction
but it returns this error,
File "<ipython-input-171-9670327f0e8e>", line 6
interaction = interaction
^
SyntaxError: invalid syntax
I am sorry that the question is so basic, but I am bummed. I've tried changing the variabel names, but it keeps returning the same error. when I changed it into return, like this,
interaction = 0
for i, row in df.iterrows():
if ((df2['names'].iloc[i] == (df2['names'].iloc[i-1]) & (df2['time'].iloc[i] == df2['time'].iloc[i-1]) & (df2['date'].iloc[i]== df2['date'].iloc[i-1]))
return interaction
else:
return (interaction+1)
return interaction
it returns this error.
File "<ipython-input-179-99d835a6b0e6>", line 5
return interaction
^
SyntaxError: invalid syntax
I deleted colon after in if after the condition because it's a syntax error as well. thank you for your help.
Edit: this is the error if I included the colon
File "<ipython-input-184-041229eea329>", line 5
& (df2['date'].iloc[i]== df2['date'].iloc[i-1])):
^
SyntaxError: invalid syntax