简体   繁体   English

在Python中从CSV创建一系列变量?

[英]Creating a series of variables from CSVs in Python?

I am trying to create a series of dictionaries from CSVs that I want to import but I am not sure the best way to do it. 我正在尝试从要导入的CSV创建一系列词典,但是我不确定做到这一点的最佳方法。

I used RatingFactors = os.listdir(RatingDirectory) and 我使用了RatingFactors = os.listdir(RatingDirectory)

CSVLocations = [] for factor in RatingFactors: CSVLocations.append(RatingDirectory + factor)

to create a list of CSVs, these CSVs contain what is essentially a dictionary of FactorName | 要创建CSV列表,这些CSV实际上包含FactorName | Dictionary的字典。 Factor Value, then 1 | 因子值,然后为1 | 5, 2 | 5、2 | 3.5. 3.5。

I want to create a dictionary for each CSV, ideally named based on the CSVs name. 我想为每个CSV创建一个字典,最好根据CSV名称命名。 However I understand that when looping across variables it is considered bad to try and name my variables inside the loop. 但是我知道,在变量之间循环时,尝试在循环内命名我的变量被认为是不好的。

I tried creating a generator function using df_from_each_file = (pd.read_csv(CSVs) for CSVs in CSVLocations) 我尝试使用df_from_each_file = (pd.read_csv(CSVs) for CSVs in CSVLocations)创建生成器函数

and if I print the generator using for y in df_from_each_file: print(y) it gives me each of the dataframes but I don't know how to separate them out? 如果我for y in df_from_each_file: print(y)使用for y in df_from_each_file: print(y)打印生成器for y in df_from_each_file: print(y)它将为我提供每个数据帧,但是我不知道如何将它们分离出来?

What is the Pythonic way to do this? 用Python的方式可以做到这一点?

How the CSVs look post import 导入后CSV的外观

0         0  1.1
1         1  0.9
2         2  0.9
3         3  0.9
etc

Edit: 编辑:

Attempt to rephrase my question. 尝试改写我的问题。

I have a series of CSVs which look like they are formatted like dictionaries, they have two columns and they represent how one factor relates to another. 我有一系列CSV,它们的格式像字典一样,有两列,它们代表一个因素与另一个因素的关系。 I would like to make a dictionary for each CSV, named like the CSV so that I can interact with them from Python. 我想为每个CSV制作一个字典,命名为CSV,以便可以从Python与它们进行交互。

Edit 2: 编辑2:

I believe this question is different than the one referenced as that is creating a single dataframe which contains all of the dictionaries, I want all of the dictionaries to be separate rather than in a single unit. 我相信这个问题与所提到的问题不同,因为它创建的是一个包含所有字典的数据框,我希望所有字典都分开而不是单个单元。 I tried using their answer before asking this and I could not separate them out. 在问这个问题之前,我尝试使用他们的答案,但无法将他们分开。

I think need dict comprehension with basename for keys: 我认为需要使用基basename dict comprehension键:

import glob, os

files = glob.glob('files/*.csv')
sers={os.path.basename(f).split('.')[0]:pd.read_csv(f,index_col=[0]).squeeze() for f in files}

If want one big Series : 如果要一个大Series

d = pd.concat(sers, ignore_index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM