仅打印/打印CSV fi中某些特定电台的特定列

2024-05-14 03:36:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个充电模拟程序,模拟不同的电动汽车到达不同的站点充电。你知道吗

当模拟完成后,程序会为充电站创建CSV文件,包括每小时的统计数据和每天的统计数据,首先,每小时的统计数据CSV对我来说很重要。你知道吗

我想为不同的车站绘制queue_length_per_hour(从0到24每小时有多少辆车在排队等候)。你知道吗

但问题是,我不想包括所有的车站,因为有太多的,所以我认为只有3个车站是不够的。你知道吗

我应该选哪3个站?我选择了3个车站,根据他们中哪一个白天到站的车最多(我可以在24小时看到这一点)

正如您在代码中看到的,我使用了pandas的filter方法,因此我可以根据CSV文件中24小时访问车辆最多的人来选择前3个站点。你知道吗

现在我有了前三个电台,现在我想画出整个栏目cars_in_queue_per_hour,不仅是24小时,而且从0小时一直往下。你知道吗

from time import sleep
import pandas as pd
import csv
import matplotlib.pyplot as plt


file_to_read = pd.read_csv('results_per_hour/hotspot_districts_results_from_simulation.csv', sep=";",encoding = "ISO-8859-1")


read_columns_of_file = file_to_read.columns

read_description = file_to_read.describe()


visited_cars_at_hour_24 = file_to_read["hour"] == 24

filtered = file_to_read.where(visited_cars_at_hour_24, inplace = True, axis=0)

top_three = (file_to_read.nlargest(3, 'visited_cars')) 
# This pick top 3 station based on how many visited cars they had during the day

#print("Top Three stations based on amount of visisted cars:\n{}".format(top_three))

#print(type(top_three))
top_one_station = (top_three.iloc[0]) # HOW CAN I PLOT QUEUE_LENGTH_PER_HOUR COLUMN FROM THIS STATION TO A GRAPH?
top_two_station = (top_three.iloc[1]) # HOW CAN I ALSO PLOT QUEUE_LENGTH_PER_HOUR COLUMN FROM THIS STATION TO A GRAPH?
top_three_station = (top_three.iloc[2]) # AND ALSO THIS?
#print(top_one_station)

#print(file_to_read.where(file_to_read["name"] == "Vushtrri"))

#for row_index, row in top_three.iterrows():
#  print(row)
#  print(row_index)
#  print(file_to_read.where(file_to_read["name"] == row["name"]))
#  print(file_to_read.where(file_to_read["name"] == row["name"]).columns)


xlabel = []
for hour in range(0,25):
    xlabel.append(hour)
ylabel = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # how to append queue length per hour for the top 3 stations here?

plt.plot(xlabel,ylabel)
plt.show()

代码也可以在这里找到复制与CSV文件一起链接:https://repl.it/@raxor2k/almost-done


Tags: csvtonamereadtopcarsfilerow
1条回答
网友
1楼 · 发布于 2024-05-14 03:36:55

我非常喜欢用seaborn-包来制作这种类型的绘图,所以我会使用

import seaborn as sns
df_2 = file_to_read[file_to_read['name'].isin(top_three['name'])]
sns.factorplot(x='hour', y='cars_in_queue_per_hour', data=df_2, hue='name')

您已经选择了前三个名称,因此唯一相关的部分是使用^{}选择数据帧中名称与前三个名称匹配的行,并让seaborn进行绘图。你知道吗

enter image description here

要使其工作,请确保通过删除inplace来更改一行代码:

filtered = file_to_read.where(visited_cars_at_hour_24, axis=0)
top_three = (filtered.nlargest(3, 'visited_cars'))

这将保留原始数据帧,以便使用来自的所有数据。如果使用inplace,则不能将其赋值回-操作在原地执行并返回None。你知道吗

我清理了绘图不需要的代码行,所以要复制的完整代码将是

import seaborn as sns
top_three = file_to_read[file_to_read['hour'] == 24].nlargest(3, 'visited_cars')
df_2 = file_to_read[file_to_read['name'].isin(top_three['name'])]
sns.factorplot(x='hour', y='cars_in_queue_per_hour', data=df_2, hue='name')

相关问题 更多 >