简体   繁体   English

绘制一张图表,计算每天的出行次数

[英]Draw a graph counting the number of trips per day

I have to build a graph of the dependence of the number of trips on the day of the year.我必须建立一个关于一年中一天的旅行次数依赖性的图表。

First, let's look at the data:首先,让我们看一下数据:

i = 0

for d in data['pickup_datetime']:
    i+=1
    print(d, type(d), sep='    ')
    if (i == 10):
        break
2016-03-14 17:24:55    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-06-12 00:43:35    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-01-19 11:35:24    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-04-06 19:32:31    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-03-26 13:30:55    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-01-30 22:01:40    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-06-17 22:34:59    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-05-21 07:54:58    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-05-27 23:12:23    <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2016-03-10 21:45:01    <class 'pandas._libs.tslibs.timestamps.Timestamp'>

To draw the desired graph above I want to use seaborn.countplot , but I don't know how to transfer the column data['pickup_datetime'] so that it displays only the day and month, and not all the time.要在上面绘制所需的图表,我想使用seaborn.countplot ,但我不知道如何传输列 data['pickup_datetime'] 以便它只显示日期和月份,而不是所有时间。

More info about data:有关数据的更多信息:

data.info()
id                    1458644 non-null object
vendor_id             1458644 non-null int64
pickup_datetime       1458644 non-null datetime64[ns]
passenger_count       1458644 non-null int64
pickup_longitude      1458644 non-null float64
pickup_latitude       1458644 non-null float64
dropoff_longitude     1458644 non-null float64
dropoff_latitude      1458644 non-null float64
store_and_fwd_flag    1458644 non-null object
trip_duration         1458644 non-null int64
log_trip_duration     1458644 non-null float64
dtypes: datetime64[ns](1), float64(5), int64(3), object(2)
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# set plt parameters
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = (16.0, 10.0)

# data
data = {'DateTime': ['2016-03-14 17:24:55', '2016-06-12 00:43:35', '2016-01-19 11:35:24', '2016-04-06 19:32:31', '2016-03-26 13:30:55', '2016-01-30 22:01:40', '2016-06-17 22:34:59', '2016-05-21 07:54:58', '2016-05-27 23:12:23', '2016-03-10 21:45:01']}

# dataframe
df = pd.DataFrame(data)

# convert to datetime
df.DateTime = pd.to_datetime(df.DateTime)

# groupby day, count and plot
g = df.groupby(df.DateTime.dt.dayofyear).count().plot.bar()
# plt.xticks(rotation=0)  # if you want to rotate the x-tick labels

在此处输入图像描述

Using sns.countplot使用sns.countplot

sns.countplot(df.DateTime.dt.dayofyear, data=df)

在此处输入图像描述

You can try creating a different column for the year, month and day您可以尝试为年、月和日创建不同的列

df = data.copy()
df['year'] = pd.DatetimeIndex(df.pickup_datetime).year
df['month'] = pd.DatetimeIndex(df.pickup_datetime).month
df['day'] = pd.DatetimeIndex(df.pickup_datetime).day

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM