Reputation: 3
I have loaded several .xlsx files as dataframes into a list from a specific folder and would like to "unpack" these dataframes within the list to the environment. I come from R where the list2env()
function would get the job done but I can't seem to find a solution for it in python (newbie).
file_names = os.listdir()
df = []
for file_names in file_names: df.append(pd.read_excel(file_names))
How do I unpack into the environment the dataframes in the 'df' list?
Thanks!
Upvotes: 0
Views: 340
Reputation: 6930
The literal equivalent of list2env
would be globals().update(...)
or globals()[...] = ...
, but generally using an explicit dictionary would be better.
As others have suggested, an explicit variable would be something like:
df = {
os.path.splitext(filename)[0]: pd.read_excel(filename)
for filename in os.listdir()
}
Then you can use df["name"]
to refer to each dataframe.
If you do want to make all of them into top-level objects, you can do that using:
globals().update(df)
or, in a loop:
for filename in os.listdir():
bare_name = os.path.splitext(filename)[0]
globals()[bare_name] = pd.read_excel(filename)
However, this will make things harder to debug, runs the risk of replacing built-in functions and modules with the loaded data frames, and will generally lead to every person who reads the code saying "please do not do that". At most, it might be OK in a notebook used for initial exploration, never in production code.
Upvotes: 1
Reputation: 28644
I'll suggest you use pathlib to filter for the specific files :
from pathlib import Path
p = Path(folder_path)
Filter for the specific suffixes :
xlsx_files = p.rglob("*.xlsx")
Iterate with pandas :
[pd.read_excel(f) for f in xlsx_files]
If you want to keep the file name, you could use a dictionary :
{f.name: pd.read_excel(f) for f in xlsx_files}
Upvotes: 1
Reputation: 109526
I believe you could use a conditional dictionary comprehension:
dfs ={filename: pd.read_excel(filename) for filename in os.listdir()
if filename.endswith('.xlsx')}
Each dataframe can then be accessed via its filename, e.g. dfs[filename]
.
Upvotes: 1