Pandas:数据框字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48013737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: Dictionary of Dataframes
提问by Adi
I have a function that I made to analyze experimental data (all individual .txt files)
我有一个用来分析实验数据的函数(所有单独的 .txt 文件)
This function outputs a dictionary ({}) of Pandas Dataframes
此函数输出 Pandas Dataframes 的字典 ({})
Is there a efficient way to iterate over this dictionary and output individual dataframes? Let's say my dictionary is called analysisdict
有没有一种有效的方法来迭代这个字典并输出单个数据帧?假设我的字典叫做 analysisdict
for key in analysisdict.keys():
dfx=pd.concat([analysisdict[key]['X'], analysisdict[key]['Y']], axis=1)
Where dfx would be an individual dataframe. (I'm guessing a second loop might be required? Perhaps I should iterate through a list of df names?)
其中 dfx 将是一个单独的数据框。(我猜可能需要第二个循环?也许我应该遍历 df 名称列表?)
The output would be df1...dfn
输出将是 df1...dfn
回答by Peter Leimbigler
EDIT: I initially misread your question, and thought you wanted to concatenate all the DataFrames into one. This does that:
编辑:我最初误读了您的问题,并认为您想将所有 DataFrame 连接成一个。这是这样做的:
dfx = pd.concat([df for df in analysisdict.values()], ignore_index=True)
(Thanks to @paul-h for the ignore_index=True
tip)
(感谢@paul-h 的ignore_index=True
提示)
I read your question more carefully and realized that you're asking how to assign each DataFrame in your dictionary to its ownvariable, resulting in separate DataFrames named df1, df2, ..., dfn
. Everything in my experience says that dynamically creating variables in this way is an anti-pattern, and best left to dictionaries. Check out the discussion here: How can you dynamically create variables via a while loop?
我更仔细地阅读了您的问题,并意识到您在询问如何将字典中的每个 DataFrame 分配给它自己的变量,从而生成名为df1, df2, ..., dfn
. 我的经验表明,以这种方式动态创建变量是一种反模式,最好留给字典。查看此处的讨论:如何通过 while 循环动态创建变量?