Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从列表中提取数据帧的名称并将其作为标题打印到输出?_Python_Python 3.x_Pandas_Python 2.7 - Fatal编程技术网

Python 如何从列表中提取数据帧的名称并将其作为标题打印到输出?

Python 如何从列表中提取数据帧的名称并将其作为标题打印到输出?,python,python-3.x,pandas,python-2.7,Python,Python 3.x,Pandas,Python 2.7,Mylist1包含4个数据帧,例如: 高分组,中分组,低分组,rba分组 我创建了一个for循环,这样每个数据帧都进入循环并提供一个输出 在打印输出之前,我希望我的代码以dataframe名称作为标题,以便能够识别结果属于哪个dataframe 示例:在打印group\u high数据帧结果之前,我希望有一个标题为group\u high,后面是group\u high的输出。类似地,我需要它用于list1中的所有其他数据帧 下面是我的代码: os.chdir(r'C:\Users\91979\

My
list1
包含4个数据帧,例如:

高分组
中分组
低分组
rba分组

我创建了一个for循环,这样每个数据帧都进入循环并提供一个输出

在打印输出之前,我希望我的代码以dataframe名称作为标题,以便能够识别结果属于哪个dataframe

示例:在打印
group\u high
数据帧结果之前,我希望有一个标题为
group\u high
,后面是
group\u high
的输出。类似地,我需要它用于
list1
中的所有其他数据帧

下面是我的代码:

os.chdir(r'C:\Users\91979\Downloads\head code\src')
from classStruct.model import model
list1 = [group_high,group_medium,group_low,group_rba]
for i in list1:
    needed_cols = i.columns
    target_col =  ['Rejection (%)']
    cols = list(set(needed_cols) - set(target_col))
    totData = i
    totData = totData.round(decimals=2)
    Model1 = model(totData,cols,['Rejection (%)'])
    clustSet = pd.DataFrame([C.clusterCenter for C in Model1.clustersList])
    Model1.predictor(clustSet, ["Rejection (%)"], Normalize=False)
    Model1.optimalClusterRejectionSeries = round(min(clustSet['Rejection (%)Predicted']),4)
    col_list = ['GCS (kg/cm2)', 'Inert Fines (%)', 'Volatile Matter (%)',
       'LOI (%)', 'Active Clay (%)', 'GFN/AFS (no)', 'Compactability (%)',
       'Wet Tensile Strength (gm/cm2)', 'Moisture (%)',
       'Permeability (no)', 'Temp. of Sand after mix.(C)']
    Model1.deNormalizeColumns(col_list, clustSet).to_csv("Predicted_optimal.csv")
    Model1.deNormalizeColumns(col_list, clustSet)
    print(pd.DataFrame(clustSet[clustSet['Rejection (%)Predicted'] == clustSet['Rejection (%)Predicted'].min()]))
    print('\n')
    print('\n')

数据帧不存储
self.name
。。。你需要自己为它提供类似的东西

for i, name in zip(list1, names_list):
您可以决定在字典中收集所有dfs

all_dfs = {name: df for name, df in zip(names_list, list1)}
# then iterate
for name, df in all_dfs.items():
通常,如果多个dfs具有相同的列且索引具有相同的级别,则最好将它们作为单个df保留,并使用
df.groubpy
在分组行上迭代(由于您的dfs似乎来自一个groupby,您可以跳过这一步,在这一步中,您可能会分成多个dfs,并直接从Grouppy进行迭代,如果这是您的情况)


编辑:

请理解,上面的所有代码块都是独占的,即选择一个解决方案并坚持使用它。您的评论试图将不同的解决方案组合在一起,并使用
all_dfs.items()
,这在任何选项中都不提供,在这种情况下也不是必需的

如果你选择第一个选项,那么

for i, name in zip(list1, names_list):
    print(name)
    needed_cols = i.columns # from your code
    # the rest of your code inside the loop
    

数据帧不存储
self.name
…您需要自己为它提供类似
for i,name in zip(列表1,names\u list):
@RichieV如果我有一个单独的列表2,其中包含打印输出前我想要的所有标题。我应该在代码的哪一部分插入它?
list2=[“Group High”,“集团中等”、“集团低”、“集团RBA”
我想我需要为列表2中的j做一些类似于
的事情:打印j
但我应该做代码的哪一部分。你看到我的答案了吗?
名称列表中的
是你的
列表2
…习惯在这种情况下使用dict,甚至在possible@RichieV我得到了输出,但是代码给出了16个结果,而我没有得到ed只有4个。只有前4个输出。从第5个到第16个是前4个输出的副本。如何删除它们?
list1=[组高,组中,组低,组rba]名称列表=[“组高:”,“组中:”,“组低:”,“组rba:”]对于i,zip中的名称(列表1,名称列表):所有的dfs={name:df代表名称,df代表zip中的df(名称列表,列表1)}对于名称,df在所有的dfs.items()中:print(name)
如果打印此代码,它会给出16个名称,但我只需要4个。您能帮助我吗
for i, name in zip(list1, names_list):
    print(name)
    needed_cols = i.columns # from your code
    # the rest of your code inside the loop