0

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
1
  • check your parentheses Commented Jun 30, 2020 at 10:56

1 Answer 1

0

You are missing : at the end of the if statement

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't use it because the colon only returns more error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.