0

I want to get the yearly mean of a list of values associated with a zip code the problem I get this error code, can you please help me out.

My DataFrame

My code is:

np.random.seed(2020)
rng = pd.date_range('2/9/2017', '3/19/2019', freq='10T')
df2 = pd.DataFrame({'SCORE':np.random.randint(1000, size=len(rng))}, index=rng)
out = pd.Series({x: df2.loc({x: df2.loc[f'02/9/{x}', :f'03/19/{x+1}', 'SCORE'].mean()
                 for x in range(df2.index.year.min(), df2.index.year.max()+1)})

The error is:

  File "<ipython-input-150-61dcc574e6e3>", line 1
    out = pd.Series({x: df2.loc({x: df2.loc[f'02/9/{x}', :f'03/19/{x+1}', 'SCORE'].mean()
                                                       ^
SyntaxError: invalid syntax
0

1 Answer 1

0

You miss a coma between f'02/9/{x}' and :f'03/19/{x+1}':

np.random.seed(2020)
rng = pd.date_range('2/9/2017', '3/19/2019', freq='10T')
df2 = pd.DataFrame({'SCORE':np.random.randint(1000, size=len(rng))}, index=rng)
out = pd.Series({x: df2.loc({x: df2.loc(f'02/9/{x}', f'03/19/{x+1}', 'SCORE').mean()
                 for x in range(df2.index.year.min(), df2.index.year.max()+1)})
Sign up to request clarification or add additional context in comments.

11 Comments

Hello Federico this did not work
@Khan, I noticed now that there was a : so I correct it, try now
File "<ipython-input-157-7a40a64464c7>", line 1 out = pd.Series({x: df2.loc({x: df2.loc[f'02/9/{x}', f'03/19/{x+1}', 'SCORE'].mean() ^ SyntaxError: invalid syntax
is if df2.loc a function then wrapp it in ()brackets
Try now, I change the code again and let me know, if it works remember to vote up my answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.