添加第三个要读取的特定数据类型?

2024-04-23 18:55:24 发布

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

我已经设法添加一个额外的特定数据类型,从我的CSV文件读取。首先:我选择我想读多少天,正如你在代码中看到的,在“分布”一栏中,我只想看“主要地区”,但现在我还想包括分布右边的一栏,名为:“地区”,只看“北部”地区的车站,换句话说,我想包括“centrum\u middle”变量,正如您在代码中看到的那样

下面是我的CSV文件的图片:

https://imgur.com/a/ZVeicp8

根据我的代码(在下面),这是可能的吗?有人能帮我吗

谢谢你

我的代码和现在一样完美,只是我想添加centrum\u middle:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

read_CSV_for_oslo_centrum_middle_days = pd.read_csv('vise_folka/ALL_stations_together.csv', sep=";", encoding ="ISO-8859-1")

day100 = read_CSV_for_oslo_centrum_middle_days[read_CSV_for_oslo_centrum_middle_days['day'] == 100]


main_district = day100[day100['Distribution'] == 'main_district']

centrum_middle = day100[day100['Region' ]== 'north'] # HOW CAN I ADD NORTH REGION AS A THIRD DATATYPE to look at?

top4_visited_stations = main_district.nlargest(4, 'total_visited_cars')

dataframes_for_centrum_middle_stations = read_CSV_for_oslo_centrum_middle_days[read_CSV_for_oslo_centrum_middle_days['name'].isin(top4_visited_stations['name'])]


sns.relplot(x='day', y='avg_queue_length', data=dataframes_for_centrum_middle_stations, hue='name',kind="line")
plt.suptitle("centrum middle")


sns.relplot(x='day', y='avg_total_EV_in_station', data=dataframes_for_centrum_middle_stations, hue='name', kind="line")
plt.suptitle("centrum middle")


plt.show()

Tags: csv代码nameimportmiddleforreadas
1条回答
网友
1楼 · 发布于 2024-04-23 18:55:24

解决了!我刚刚把region变量放在分布变量上,它工作得很好!代码如下:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

read_CSV_for_oslo_centrum_middle_days = pd.read_csv('vise_folka/ALL_stations_together.csv', sep=";", encoding ="ISO-8859-1")

day100 = read_CSV_for_oslo_centrum_middle_days[read_CSV_for_oslo_centrum_middle_days['day'] == 100]




west = day100[day100['Region' ]== 'west'] # HOW CAN I ADD NORTH REGION AS A THIRD DATATYPE to look at?

main_district = west[west['Distribution'] == 'main_district']


top4_visited_stations = main_district.nlargest(4, 'total_visited_cars')

dataframes_for_centrum_middle_stations = read_CSV_for_oslo_centrum_middle_days[read_CSV_for_oslo_centrum_middle_days['name'].isin(top4_visited_stations['name'])]


sns.relplot(x='day', y='avg_queue_length', data=dataframes_for_centrum_middle_stations, hue='name',kind="line")
plt.suptitle("west")


sns.relplot(x='day', y='avg_total_EV_in_station', data=dataframes_for_centrum_middle_stations, hue='name', kind="line")
plt.suptitle("west")


plt.show()

相关问题 更多 >