0

This is the piece of code here. It deals with the data analysis of the avocado prices dataset avaialble on kaggle using pandas.

graph_df = pd.DataFrame
for r in regions:
  r_df = df.copy()[df['region']==r]
  r_df.set_index("Date", inplace=True)
  r_df.sort_index(inplace=True)
  r_df[f"{r}_25MA"] = r_df['AveragePrice'].rolling(25).mean()

  if graph_df.empty:
    graph_df = r_df[[f"{r}_25MA"]]
  else:
    graph_df = graph_df.join(r_df[f"{r}_25MA"])

graph_df.tail()

what i want to know is what does the letter 'f' before {r}_25MA do in this

r_df[f"{r}_25MA"] = r_df['AveragePrice'].rolling(25).mean()

  if graph_df.empty:
    graph_df = r_df[[f"{r}_25MA"]]
  else:
    graph_df = graph_df.join(r_df[f"{r}_25MA"])

if anybody knows please do help and sorry for asking a noob question.

  • It's [string interpolation](https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498) also called `f-string`s. – Sean Vieira Aug 05 '20 at 12:43

1 Answers1

0

f-strings are a great new way to format strings. For example:

text = "is text"
print(f"This {text}")
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Aryasatya
  • 36
  • 1
  • 3