我有以下数据集:
dataset.head(7)
Transaction_date Product Product Code Description
2019-01-01 A 123 A123
2019-01-02 B 267 B267
2019-01-09 B 267 B267
2019-02-11 C 139 C139
2019-02-11 A 125 C125
2019-02-12 C 139 C139
2019-02-12 A 123 A123
数据集存储有交易日期的交易信息。换句话说,并非每天都有数据可用。 最终,我想创建一个时间序列图,向我显示每天的交易数量。
到目前为止,我已经完成了一个简单的计数图:
ax = sns.countplot(x=dataset["Transaction_date"],data=dataset)
此图显示了发生交易的日期。但是我更希望看到日期,在图中没有交易发生,最好显示为0。
我尝试了以下操作,但检索到错误消息:
groupbydate = dataset.groupby("Transaction_date")
ax = sns.tsplot(x="Transaction_date",y="Product",data=groubydate.fillna(0))
但是我得到了错误
cannot label index with a null key
由于限制,我只能使用seaborn 0.8.1
答案 0 :(得分:0)
我相信reindex
应该为您服务:
# First convert the index to datetime
dataset.index = pd.DatetimeIndex(dataset.index)
# Then reindex! You can also select the min and max of the index for the limits
dataset= dataset.reindex(pd.date_range("2019-01-01", "2019-02-12"), fill_value="NaN")
答案 1 :(得分:0)
您可以使用pandas.DataFrame.dropna
删除包含NaN值的行,然后绘制图表。例如:
dataset.dropna(thresh=2)
将删除至少有两个NaN值的所有行。
您可能还想使用pandas.DataFrame.fillna