-2

I'm writing a function to import .dta files that will have the name structure but with different prefixes. How do I reference this prefix in a string filepath name? For example, something like

 def import_stata(code):
    df=pd.read_stata("code_dataset.dta")
    return df

Where code is the prefix that will change. Right now I'm getting an error saying that code_dataset doesn't exist.

Thanks!

yogz123
  • 703
  • 3
  • 8
  • 25

1 Answers1

0

Use the following string concatenation:

def import_stata(code):
  df=pd.read_stata(code+"_dataset.dta")
  return df
Günther Jena
  • 3,706
  • 3
  • 34
  • 49