You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取文件夹中各文件并创建对应名称的独立DataFrame?

Solution: Create Individual DataFrames for Each CSV File

Instead of concatenating all files into a single DataFrame, you can use a dictionary to store each DataFrame under a key corresponding to the file name. This is the cleanest and most maintainable approach in Python.

Here's how to modify your code to create separate DataFrames stored in a dictionary:

import glob
import pandas as pd

path = r'C:\Users\SemR\Documents\Jupyter\Submissions'
all_files = glob.glob(path + "/*.csv")

# Initialize an empty dictionary to hold your DataFrames
df_dict = {}

for filename in all_files:
    # Extract the base filename (without path and .csv extension)
    # Split on backslashes (Windows path) and take the last part, then remove .csv
    file_name = filename.split('\\')[-1].replace('.csv', '')
    
    # Read the CSV into a DataFrame, keeping only your desired columns
    df = pd.read_csv(filename, index_col=None, header=0, usecols=['Date', 'Usage'])
    
    # Store the DataFrame in the dictionary using the filename as the key
    df_dict[file_name] = df

How to Access Your DataFrames

Once the code runs, you can access each DataFrame by its filename using the dictionary:

# Example: Access the DataFrame for a file named "January_Data.csv"
df_dict['January_Data']

You can list all available DataFrame names with:

print(df_dict.keys())

If you specifically want separate variables named after each file (rather than using a dictionary), you can use globals() to dynamically create variables. However, this approach is not ideal because it can clutter your namespace, cause name conflicts, and make it harder to manage multiple DataFrames.

Here's how you'd do it:

import glob
import pandas as pd

path = r'C:\Users\SemR\Documents\Jupyter\Submissions'
all_files = glob.glob(path + "/*.csv")

for filename in all_files:
    file_name = filename.split('\\')[-1].replace('.csv', '')
    df = pd.read_csv(filename, index_col=None, header=0, usecols=['Date', 'Usage'])
    # Dynamically create a variable with the filename
    globals()[file_name] = df

Warnings for This Approach:

  • If your filenames contain spaces, hyphens, or special characters, they'll become invalid variable names (e.g., "January Data.csv" would try to create a variable named January Data, which is not allowed).
  • You risk overwriting existing variables if a filename matches an existing variable name.
  • It's difficult to track all the DataFrames you've created without manually checking the namespace.

Stick with the dictionary approach for a cleaner, more manageable solution.

内容的提问来源于stack exchange,提问作者R Sem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:45:35