Im working on preprocessing a large dataset and I have put all preprocessing steps in a function. The problem is that now, the dataset in this function is named as df
, and when I would loop over this function, then all my datasets are called the same. I want to automatically rename the dataframe in this function.
To solve this, I found a way in which I can extract the filename:
filename = r"C:\Users\Celin\OneDrive\Documenten\Multi Channel DataManager\week9\week10_420_R_spike.csv"
filename2= Path(filename)
df_name = filename2.name.split('/') #this gives you the filename
df_name is a list with one element.
when I print(df_name)
it gives me ['week10_420_R_spike.csv']
and when I print(df_name[0])
it gives me week10_420_R_spike.csv
.
However, when I try to use this to rename my dataset:
df = pd.read_csv(r"C:\Users\Celin\OneDrive\Documenten\Multi Channel DataManager\week9\week10_420_R_spike.csv")
df_name[0] = pd.DataFrame(df)
Then I dont see any dataframe called week10_420_R_spike and trying to print the dataframe with print(week10_420_R_spike)
gives me the error: NameError: name 'week10_420_R_spike' is not defined
I also tried to use .rename:
df.rename = df_name[0]
But that did not work either.
Any solutions?