我有一个CSV文件,每个单元格中填充了日期,我想计算每个日期有多少个,然后我将使用matplotlib绘制到条形图上
我不知道如何计算每个日期有多少个实例
我有以下代码来读取csv文件,但不确定从这里去哪里
def Readtoarray():
with open('Book1.csv','r') as file:
reader = csv.reader(file, delimiter=',')
next(reader, None) # skip the headers
for row in reader:
XXXXXXXXX
示例数据:
23/03/2020,6630997
23/03/2020,6630990
20/03/2020,6630390
20/03/2020,6630386
我想最简单的方法是将csv文件作为数据帧读取,然后使用
value_counts()
函数Count the frequency that a value occurs in a dataframe column
我建议使用计数器dict对象(请参见https://docs.python.org/3/library/collections.html#collections.Counter)
这将导致以下输出:
你能提供一个你的CSV日期的例子吗? 要计算Python中某些内容的出现次数,最好的方法是使用^{}
编辑
将日期作为行中的第一个值,假设您不需要对这些日期进行特殊处理,这意味着要解析它们:
它将返回一个dict,其中日期(作为字符串,例如
"23/03/2020"
)作为键,其出现次数作为值。如果希望日期作为datetime.date
对象,可以在理解列表中解析它们:相关问题 更多 >
编程相关推荐