如何在python中的for循环中打印输出之前设置标题?
我在列表1中有4个如何在python中的for循环中打印输出之前设置标题?,python,python-3.x,pandas,python-2.7,Python,Python 3.x,Pandas,Python 2.7,我在列表1中有4个df: df1df2df3df4 list1中的每个数据帧进入for循环并提供单个输出,但在打印输出之前,我希望给出一个标题,以便能够识别输出所属的组。i、 e是否属于高、低、中或平均水平 list2包含4个输出需要给出的所有标题。 list2=[“高”、“中”、“低”、“平均”] 例如: Before df1 output is printed i wanted the heading to be 'High' Before df2 output is
df
:
df1
df2
df3
df4
list1
中的每个数据帧进入for循环并提供单个输出,但在打印输出之前,我希望给出一个标题,以便能够识别输出所属的组。i、 e是否属于高、低、中或平均水平
list2
包含4个输出需要给出的所有标题。
list2=[“高”、“中”、“低”、“平均”]
例如:
Before df1 output is printed i wanted the heading to be 'High'
Before df2 output is printed i wanted the heading to be 'Medium'
Before df3 output is printed i want the heading to be printed as 'Low'
Before df4 output is printed i want the heading to be printed as 'Average'
下面是我的代码:
import os
os.chdir(r'C:\Users\91979\Downloads\head code\src')
from classStruct.model import model
list1 = [df1,df2,df3,df4]
list2 = ["High","Medium","Low", "Average"]
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)
for j in list2:
print(j)
print(pd.DataFrame(clustSet[clustSet['Rejection (%)Predicted'] == clustSet['Rejection (%)Predicted'].min()]))
print('\n')
print('\n')
我应该在代码的哪个部分打印list2
中的标题,以便获得正确标题的4次迭代。您可以使用zip:
list1 = [df1,df2,df3,df4]
list2 = ["High","Medium","Low", "Average"]
for i, j in zip(list1, list2):
print('Before output is printed i wanted the heading to be %s' % j)
print('This is the dataframe %s' % i)
然后,在打印标题后,您可以稍后在循环中打印数据帧i
示例标题输出:
Before output is printed i wanted the heading to be High
This is the data frame df1
Before output is printed i wanted the heading to be Medium
This is the data frame df2
Before output is printed i wanted the heading to be Low
This is the data frame df3
Before output is printed i wanted the heading to be Average
This is the data frame df4
非常感谢你。成功了:)谢谢你的帮助time@Thanuja不客气。一个更好的感谢方式是向上投票:)