使用 pandas 索引级别作为输出文件名的一部分

Use level of pandas index as a part of an output file name

我正在尝试使用

遍历数据帧
for index, row in df.iterrows():
   #Do Stuff

我有一个 pandas 数据框,如下所示:

                  pt_ID Easting Northing         Attribute
Site  Survey_Date                                         
M006R 2004-12-30   1019  245473   651608  WE              
      2004-12-30   1038  245563   651543  WE              
      2004-12-30   1017  245471   651593  WE              
      2004-12-30   1021  245482   651616  WE              
      2004-12-30   1023  245478   651604  WE              
      2004-12-30   1049  245665   651498  WE              
      2004-12-30   1020  245477   651614  WE              
      2004-12-30   1041  245577   651528  WE              
      2004-12-30   1040  245574   651535  WE              
      2004-12-30   1037  245552   651544  WE   

如蜜蜂所见,我有一个使用 SiteSurvey_Date 的多重索引。当它循环遍历数据框时,我想使用 Survey_Date 作为输出文件名的一部分,并且不知道如何在它循环遍历数据框时访问它。

例如,我想要一个输出文件名:

outfilename = 'test_' + Survey_Date + '.extention'

这可能吗?

运行 这是为了获得更好的想法:

for index, row in df.head().iterrows():
    print "Site: {}, Survey_Date {}".format(*index)

本质上,index 变量是一个包含多索引每个级别的元组。使用 index[0] for 'Site'index[1] for 'Survey_Date'

访问您想要的级别