如何循环输出每一行
How do I output each row in a loop
此代码的 objective 是在列出天数时打印出一些列值。我得到的输出是第一天的列值。
days = final_merged_data['Day'].tolist()
for day in days:
each_row = final_merged_data[final_merged_data['Day'] == days]
message_partition = f"""
[{day}]
Solar_Power_Generated in MW = {str(each_row['Power_Solar'].tolist()[0])}
Wind_Power_Generated in MW = {str(each_row['Power_Wind'].tolist()[0])}
Total_Power_Generated in MW = {str(each_row['Total MW'].tolist()[0])}"""
print(message_partition)
我得到的输出与第 1 行的列值相同。
[12]
Solar_Power_Generated in MW = 26.48
Wind_Power_Generated in MW = 30.34
Total_Power_Generated in MW = 56.82
[13]
Solar_Power_Generated in MW = 26.48
Wind_Power_Generated in MW = 30.34
Total_Power_Generated in MW = 56.82
Day Unnamed: 0.1 Temp_Hi Temp_Low Cloud Solar Month windspeed Direction Rainfall Power_Solar Power_Wind
0 16 0.0 29.44 29.01 1.0 12.24 9.0 11.50 225.0 4.98 0.3500 33.55
1 17 1.0 29.47 28.85 3.0 12.54 9.0 10.64 223.0 3.55 1.0100 31.56
2 18 2.0 29.60 28.89 51.0 12.87 9.0 10.44 224.0 4.00 16.8900 31.70
3 19 3.0 29.37 29.12 100.0 11.99 9.0 11.32 227.0 5.16 0.6618 34.13
4 20 4.0 29.32 28.90 42.0 12.50 9.0 12.18 230.0 3.50 13.9100 36.53
5 21 5.0 29.25 28.67 75.0 13.23 9.0 11.97 228.0 2.92 24.8300 35.42
6 22 6.0 29.14 28.56 66.0 12.57 9.0 11.71 229.0 1.47 21.8500 35.48
7 23 7.0 29.32 28.66 43.0 12.45 9.0 12.37 226.0 0.11 7.1200 35.13```
像这样的东西应该适用于列字典:
rows = final_merged_data.tolist()
for row in rows:
message_partition = f"""
[{row['Day']}]
Solar_Power_Generated in MW = {str(row['Power_Solar'])}
Wind_Power_Generated in MW = {str(row['Power_Wind'])}
Total_Power_Generated in MW = {str(row['Total MW'])}"""
print(message_partition)
或者,如果所有列都存储为字典中的列表,
rows = zip(final_merged_data['Day'].tolist(), final_merged_data['Power_Solar'].tolist(), final_merged_data['Power_Wind'].tolist(), final_merged_data['Total MW'].tolist())
for row in rows:
message_partition = f"""
[{row[0]}]
Solar_Power_Generated in MW = {str(row[1])}
Wind_Power_Generated in MW = {str(row[2])}
Total_Power_Generated in MW = {str(row[3])}"""
print(message_partition)
此代码的 objective 是在列出天数时打印出一些列值。我得到的输出是第一天的列值。
days = final_merged_data['Day'].tolist()
for day in days:
each_row = final_merged_data[final_merged_data['Day'] == days]
message_partition = f"""
[{day}]
Solar_Power_Generated in MW = {str(each_row['Power_Solar'].tolist()[0])}
Wind_Power_Generated in MW = {str(each_row['Power_Wind'].tolist()[0])}
Total_Power_Generated in MW = {str(each_row['Total MW'].tolist()[0])}"""
print(message_partition)
我得到的输出与第 1 行的列值相同。
[12]
Solar_Power_Generated in MW = 26.48
Wind_Power_Generated in MW = 30.34
Total_Power_Generated in MW = 56.82
[13]
Solar_Power_Generated in MW = 26.48
Wind_Power_Generated in MW = 30.34
Total_Power_Generated in MW = 56.82
Day Unnamed: 0.1 Temp_Hi Temp_Low Cloud Solar Month windspeed Direction Rainfall Power_Solar Power_Wind
0 16 0.0 29.44 29.01 1.0 12.24 9.0 11.50 225.0 4.98 0.3500 33.55
1 17 1.0 29.47 28.85 3.0 12.54 9.0 10.64 223.0 3.55 1.0100 31.56
2 18 2.0 29.60 28.89 51.0 12.87 9.0 10.44 224.0 4.00 16.8900 31.70
3 19 3.0 29.37 29.12 100.0 11.99 9.0 11.32 227.0 5.16 0.6618 34.13
4 20 4.0 29.32 28.90 42.0 12.50 9.0 12.18 230.0 3.50 13.9100 36.53
5 21 5.0 29.25 28.67 75.0 13.23 9.0 11.97 228.0 2.92 24.8300 35.42
6 22 6.0 29.14 28.56 66.0 12.57 9.0 11.71 229.0 1.47 21.8500 35.48
7 23 7.0 29.32 28.66 43.0 12.45 9.0 12.37 226.0 0.11 7.1200 35.13```
像这样的东西应该适用于列字典:
rows = final_merged_data.tolist()
for row in rows:
message_partition = f"""
[{row['Day']}]
Solar_Power_Generated in MW = {str(row['Power_Solar'])}
Wind_Power_Generated in MW = {str(row['Power_Wind'])}
Total_Power_Generated in MW = {str(row['Total MW'])}"""
print(message_partition)
或者,如果所有列都存储为字典中的列表,
rows = zip(final_merged_data['Day'].tolist(), final_merged_data['Power_Solar'].tolist(), final_merged_data['Power_Wind'].tolist(), final_merged_data['Total MW'].tolist())
for row in rows:
message_partition = f"""
[{row[0]}]
Solar_Power_Generated in MW = {str(row[1])}
Wind_Power_Generated in MW = {str(row[2])}
Total_Power_Generated in MW = {str(row[3])}"""
print(message_partition)