[英]Dictionary to global environment python
I have attempted to open a list of cvs files in a loop using python.我试图使用 python 在循环中打开一个 cvs 文件列表。 I started with the idea to open the files to a dictionary as I been told to do not attempt to dynamically create names on the fly, I tried the following code
我从将文件打开到字典的想法开始,因为有人告诉我不要尝试动态创建名称,我尝试了以下代码
filenames = ["broaderRelationsSkillPillar.csv","ISCOGroups_en.csv"]
dataframes ={} ## create a dictionary
for i in filenames :
dataframes[i] = pd.read_csv(i)
for k ,v in dataframes.items():
[k] = pd.DataFrame.from_dict(dataframes[k])
note - my issue happens here ,my result here is only one of the 2 data frames注意 - 我的问题发生在这里,我的结果只是 2 个数据帧之一
would I be able to open this csv files directly froma loop and name them on the fly?我可以直接从循环中打开这个 csv 文件并动态命名它们吗? I have around 20 csv's and I'm trying to automate a bit the code.
我有大约 20 个 csv,我正在尝试自动化一些代码。 tks
tks
d = {'col1': [1, 2], 'col2': [3, 4]}
a = {'col3': [1, 2], 'col4': [3, 4]}
c = {'col3': [1, 2], 'col4': [3, 4]}
d= pd.DataFrame(data=d)
a= pd.DataFrame(data=a)
c= pd.DataFrame(data=c)
filenames = [a ,d ,c]
dataframes ={} ## create a dictionary
for i in filenames :
dataframes[i] = i
del a , c, d
for k ,v in dataframes.items():
k = pd.from_dict(dataframes[k])
I believe need dict comprehension
for dictionary of DataFrames with keys by filenames:我相信需要对带有文件名键的 DataFrame 字典进行
dict comprehension
:
dataframes = {i:pd.read_csv(i) for i in filenames}
print (dataframes['broaderRelationsSkillPillar.csv'])
print (dataframes['ISCOGroups_en.csv'])
Or is possible removing last .csv
by indexing:或者可以通过索引删除最后一个
.csv
:
dataframes = {i[:-4]: pd.read_csv(i) for i in filenames}
print (dataframes['broaderRelationsSkillPillar'])
print (dataframes['ISCOGroups_en'])
Sample DataFrames:示例数据帧:
df1 = pd.DataFrame({'A': ['a','a'],'B': list(range(2))})
df2 = pd.DataFrame({'C': ['b','f','s'],'D': list(range(3))})
df3 = pd.DataFrame({'E': ['f','g','h'],'F': list(range(3))})
print (df1)
A B
0 a 0
1 a 1
print (df2)
C D
0 b 0
1 f 1
2 s 2
print (df3)
E F
0 f 0
1 g 1
2 h 2
Created dictionary of DataFrames
:创建
dictionary of DataFrames
:
dataframes = {'file1':df1, 'file2':df2, 'file3':df3}
print (dataframes)
{'file1': A B
0 a 0
1 a 1, 'file2': C D
0 b 0
1 f 1
2 s 2, 'file3': E F
0 f 0
1 g 1
2 h 2}
For DataFrame
select by key
- ere by file1
:对于
DataFrame
通过key
-ere 通过file1
:
print (dataframes['file1'])
A B
0 a 0
1 a 1
In loop v
is DataFrame
:在循环
v
是DataFrame
:
for k ,v in dataframes.items():
print (k)
print (v)
print (type(v))
file1
A B
0 a 0
1 a 1
<class 'pandas.core.frame.DataFrame'>
file2
C D
0 b 0
1 f 1
2 s 2
<class 'pandas.core.frame.DataFrame'>
file3
E F
0 f 0
1 g 1
2 h 2
<class 'pandas.core.frame.DataFrame'>
If want modify DataFrames
in loop, you need to reference the original df
by using key
of dictionary
:如果要在循环中修改
DataFrames
,则需要使用dictionary
key
引用原始df
:
for k ,v in dataframes.items():
#modify df - e.g. add `a` to first column
v.iloc[:, 0] = v.iloc[:, 0] + 'a'
print (v)
dataframes[k] = v
A B
0 aa 0
1 aa 1
C D
0 ba 0
1 fa 1
2 sa 2
E F
0 fa 0
1 ga 1
2 ha 2
Dictionary of DataFrames
: Dictionary of DataFrames
:
print (dataframes)
{'file1': A B
0 aa 0
1 aa 1, 'file2': C D
0 ba 0
1 fa 1
2 sa 2, 'file3': E F
0 fa 0
1 ga 1
2 ha 2}
Check one DataFrame
:检查一个
DataFrame
:
print (dataframes['file1'])
A B
0 aa 0
1 aa 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.