I already tried to use the os.path function.
For example, I have this file path:
/Users/GSteve_105/Documents/sites/Docs/Experiment/file1.csv
And I want to shorten it to
/file1.csv
So that I can then use the pd.read module to read the file. For example, I'd like to do this:
df1 = pd.read_csv("/Users/GSteve_105/Documents/sites/Docs/Experiment/file1.csv")
df2 = pd.read_csv("/Users/GSteve_105/Documents/sites/Docs/Experiment/file2.csv")
df3 = pd.read_csv("/Users/GSteve_105/Documents/sites/Docs/Experiment/file3.csv")
df4 = pd.read_csv("/Users/GSteve_105/Documents/sites/Docs/Experiment/file4.csv")
df5 = pd.read_csv("/Users/GSteve_105/Documents/sites/Docs/Experiment/file5.csv")
df6 = pd.read_csv("/Users/GSteve_105/Documents/sites/Docs/Experiment/file6.csv")
df7 = pd.read_csv("/Users/GSteve_105/Documents/sites/Docs/Experiment/file7.csv")
df8 = pd.read_csv("/Users/GSteve_105/Documents/sites/Docs/Experiment/file8.csv")
However, obviously 2 things are in my way:
Is there a way to do this? Thanks in advance!
Instead manually of defining variables like that, you can use a for loop to define,
and append the variables into a list.
df = []
amt = 8 # The amount of properly labeled csv files
for n in range(amt):
d = pd.read_csv(f"/Users/GSteve_105/Documents/sites/Docs/Experiment/file{n+1}.csv")
df.append(d)
If you want to call df3
, you can call df[3]
, if you want df5
, call df[5]
, etc.
You can also use the glob
module:
import glob
files = glob.glob("/Users/GSteve_105/Documents/sites/Docs/Experiment/file*.csv") # List all the csv file in the Experiment folder that begins with 'file'