我有数据框,我需要绘制并查看哪个类别得分最高。这是数据框架代码,如果绘图可以按不同颜色的类别重叠,那将是很好的。
import pandas as pd
import datetime
idx = pd.date_range('02-28-2018', '04-29-2018')
df = pd.DataFrame(
[[ '101', '2018-03-29', 'Smooth','9.0'], [
'102', '2018-03-29', 'Hard','1.0',
], [ '103', '2018-03-30', 'Narrow','1.1'], [
'104', '2018-04-30', 'Sharp','2.0'
], [ '105', '2018-04-21', 'Narrow','2.2'],[ '105', '2018-05-21', 'Narrow','2.1'],[ '105', '2018-05-22', 'Narrow','2.0']],
columns=[ 'accountid', 'timestamp', 'category','score'])
df['timestamp'] = pd.to_datetime(df['timestamp'])
df=df.set_index(['timestamp'])
print(df)
accountid category score
timestamp
2018-03-29 101 Smooth 9.0
2018-03-29 102 Hard 1.0
2018-03-30 103 Narrow 1.1
2018-04-30 104 Sharp 2.0
2018-04-21 105 Narrow 2.2
2018-05-21 105 Narrow 2.1
2018-05-22 105 Narrow 2.0
答案 0 :(得分:0)
df['score'] = df['score'].astype('float')
df.reset_index().set_index(['timestamp', 'category'])['score'].unstack('category').plot(stacked=True)
答案 1 :(得分:0)
为避免重复的行问题,在创建索引时会附加时间戳和类别列。
df['score'] = df['score'].astype('float')
df.reset_index().set_index(['timestamp', 'category'],append=True) \
['score'].unstack('category').plot(stacked=True)
plt.show()