Python: Looping through directory and saving each file using filename as data frame name

Oli*_*via 5 python loops assign pandas

In R there is a function called assign which assigns a value to a name in the environment.

EG:

assign("Hello", 2)
> Hello
[1] 2
Run Code Online (Sandbox Code Playgroud)

In python I can't seem to do the same. I initially tried:

import numpy as np
import pandas as pd
import os

for file in os.listdir('C:\\Users\\Olivia\\Documents'):
    if file.endswith(".csv"):
        os.path.splitext(file)[0] = pd.read_csv('C:\\Users\\Olivia\\Documents\\' + file)
Run Code Online (Sandbox Code Playgroud)

But I can see this is trying to make a string equal to a file which doesn't work.

I managed to get all the files in a list by doing:

import glob

dl = glob.glob(r'C:\Users\Olivia\Documents\*.csv')
nl = []
for i in dl:
    pl = i.split(os.sep)
    name = pl[5][:-4]
    nl.append(name)

ddict = {}

 for k, v in zip(nl,dl):
    ddict[k] = ddict.get(k,"") + v

 dfl = []

 for k, v in ddict.items():
    dfl.append(read_csv(v))
Run Code Online (Sandbox Code Playgroud)

But now how do I get each data frame out of the list and named as the file without the extension. There must be a way to assign each data frame in the list as a name from the file list

cs9*_*s95 10

老实说,您使用第一种方法走在正确的轨道上。不幸的是,python 没有为您提供动态创建“可变数量的变量”的选项,正如您已经尝试并已经意识到的那样。然而!您可以根据需要创建字典并将数据帧分配给字符串键。就是这样。

root = 'C:\\Users\\Olivia\\Documents'

ddict = {}
for file in os.listdir(root):
    if file.endswith(".csv"):
        name = os.path.splitext(file)[0]
        ddict[name] = pd.read_csv(os.path.join(root, file))
Run Code Online (Sandbox Code Playgroud)

构建这本词典的另一种方法是使用字典理解

ddict = {os.path.splitext(file)[0] : pd.read_csv(os.path.join(root, file)) 
                for file in os.listdir(root) if file.endswith('csv')
}
Run Code Online (Sandbox Code Playgroud)

现在,引用单个数据帧就像

ddict['your_file_name']
Run Code Online (Sandbox Code Playgroud)

另一件需要注意的事情是,加入文件最安全的方法是使用os.path.join. 它只是比普通的更安全+


参考


归档时间:

查看次数:

4273 次

最近记录:

8 年 前