问题描述
我有一个房价数据集-房价数据.当我在Numpy数组中使用数据的子集时,可以在以下很好的时间序列图中绘制它:
I have a data set of house prices - House Price Data. When I use a subset of the data in a Numpy array, I can plot it in this nice timeseries chart:
但是,当我在熊猫系列"中使用相同的数据时,图表会变得像这样的块状:
However, when I use the same data in a Panda Series, the chart goes all lumpy like this:
如何使用Panda Series创建平滑的时间序列折线图(如第一幅图像)?
How can I create a smooth time series line graph (like the first image) using a Panda Series?
这是我要获取美观的时间序列图的方法(使用Numpy数组)(在将numpy导入np,将pandas导入pd和将matplotlib.pyplot导入plt之后):
Here is what I am doing to get the nice looking time series chart (using Numpy array)(after importing numpy as np, pandas as pd and matplotlib.pyplot as plt):
data = pd.read_csv('HPI.csv', index_col='Date', parse_dates=True) #pull in csv file, make index the date column and parse the dates
brixton = data[data['RegionName'] == 'Lambeth'] # pull out a subset for the region Lambeth
prices = brixton['AveragePrice'].values # create a numpy array of the average price values
plt.plot(prices) #plot
plt.show() #show
这是我要使用熊猫系列获得块状蛋糕的方法:
Here is what I am doing to get the lumpy one using a Panda series:
data = pd.read_csv('HPI.csv', index_col='Date', parse_dates=True)
brixton = data[data['RegionName'] == 'Lambeth']
prices_panda = brixton['AveragePrice']
plt.plot(prices_panda)
plt.show()
如何使第二张图显示为平滑的适当时间序列?
How do I make this second graph show as a nice smooth proper time series?
*这是我的第一个StackOverflow问题,因此请大声喊叫我是否遗漏了一些东西或不清楚*
任何帮助表示赞赏
推荐答案
文件中的日期格式为日/月/年.为了让熊猫正确地解释此格式,您可以在read_csv
调用内使用选项dayfirst=True
.
The date format in the file you have is Day/Month/Year. In order for pandas to interprete this format correctly you can use the option dayfirst=True
inside the read_csv
call.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data/UK-HPI-full-file-2017-08.csv',
index_col='Date', parse_dates=True, dayfirst=True)
brixton = data[data['RegionName'] == 'Lambeth']
prices_panda = brixton['AveragePrice']
plt.plot(prices_panda)
plt.show()
这篇关于 pandas 系列未绘制到时间序列图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!