简体   繁体   English

如何计算和使用 python 从调查中收到的 plot 数据

[英]How can calculate & plot data received from survey using python

What I need to do is basically calculate the responses received over a period of time.我需要做的基本上是计算一段时间内收到的回复。

IE IE 在此处输入图像描述

07/07/2019 |  6
08/07/2019 |  7

And plot the above to a graph.和 plot 上面来一张图。

But the current data is in the below format:但当前数据格式如下:

07/07/2019 17:33:07
07/07/2019 12:00:03
08/07/2019 21:10:05
08/07/2019 20:06:09

So far,至今,

import pandas as pd
df = pd.read_csv('survey_results_public.csv')
df.head()
df['Timestamp'].value_counts().plot(kind="bar")
plt.show()

But the above doesn't look good.但是上面的看起来不太好。

You are counting all values in the timestamp column so you will have 1 response per timestamp.您正在计算时间戳列中的所有值,因此每个时间戳都有 1 个响应。

You should parse the timestamp column, check the unique dates and then count the number of timestamps that belong to each date.您应该解析时间戳列,检查唯一日期,然后计算属于每个日期的时间戳数。

Only then should you plot the data.只有这样你才应该 plot 数据。

So do something like this:所以做这样的事情:

import pandas as pd
import datetime

def parse_timestamps(timestamp):
    datetime.datetime.strptime(timestamp, '%d/%m/%Y %H:%M:%S')

df = pandas.read_csv('survey_results_public.csv')

df["Date"]=df["Timestamp"].map(lambda t: parse_timestamps(t).date())

df["Date"].value_counts().plot(kind="bar")

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

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