Home > Software engineering >  read csv file using string from another df(pandas, python, dataframe)
read csv file using string from another df(pandas, python, dataframe)

Time:10-10

is it possible to read csv file using string from another df?

normally, to read a csv file, i'd use the code as follow:

df = pd.read_csv("C:/Users/Desktop/file_name.csv")

however, i'd like to automate reading a csv file using string from another df:

df1_string = df1.iloc[0]['file_name']
df2 = pd.read_csv("C:/Users/Desktop/df1_string.csv")

i got a FileNotFoundError when i tried the above code: FileNotFoundError: [Errno 2] File b'C:/Users/Desktop/df1_string,csv' does not exist

kindly advices, many thanks

CodePudding user response:

Use python string formatting:

df1_string = df1.iloc[0]['file_name']
df2 = pd.read_csv(f"C:/Users/Desktop/{df1_string }.csv")
  • Related