从带有 Pandas 的函数返回多个数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45319422/
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
Return multiple DataFrames from a function with Pandas
提问by KKobain
I am trying to parse multiple excel sheets with Pandas into separate individual DataFrames.
我正在尝试将 Pandas 的多个 Excel 工作表解析为单独的单个 DataFrame。
My code so far is:
到目前为止我的代码是:
sheet_names =[tab1, tab2]
df_names = [1,2]
def initilize_dataframes(sheet_names):
for name in sheet_names:
df = xls_file.parse(name) #parse the xlxs sheet
df = df.transpose() #transpose dates to index
new_header = df.iloc[0] #column header names
df = df[1:] #drop 1st row
df.rename(columns=new_header, inplace= True) #rename the columns
return df`
`
for i in df_names:
df_(i) = initilize_dataframes(sheet_names)#something like this idk
The last two lines I can not wrap my head around. I get that the function will return the df, but I would like it to take the values from the df_names list. And label the DataFrame accordingly.
最后两行我无法理解。我知道该函数将返回 df,但我希望它从 df_names 列表中获取值。并相应地标记 DataFrame。
For example, tab1 in the excel sheet the DataFrame should be named df_1 and looping for tab2 and df_2 respectively.
例如,excel表格中的tab1,DataFrame应命名为df_1,并分别为tab2和df_2循环。
采纳答案by jezrael
It is possible by globals
:
可以通过globals
:
for i, val in enumerate(df_names):
globals()['df_' + str(vals)] = initilize_dataframes(sheet_names[i])
But better is use dict
of DataFrames
, sheet_names
select by positions from enumerate
, but need substract 1
, because python counts from 0
:
但更好的是使用dict
的DataFrames
, sheet_names
从位置选择enumerate
,但需要。减去1
,因为蟒蛇数从0
:
dfs = {}
for i, val in enumerate(df_names):
dfs[val] = initilize_dataframes(sheet_names[i])
print (dfs[1])