本周我决定深入学习pandas。我有一个包含历史IRC日志的pandas DataFrame,它看起来像这样:
我想要的是像这样的滚动计数:
timestamp action nick message
2005-11-04 01:44:33 False hack-cclub lex, hey!
2005-11-04 01:44:43 False hack-cclub lol, yea thats broke
2005-11-04 01:44:56 False lex Slashdot - Updated 2005-11-04 00:23:00 | Micro...
2005-11-04 01:44:56 False hack-cclub lex slashdot
2005-11-04 01:45:12 False lex port 666 is doom - doom Id Software (or mdqs o..
2005-11-04 01:45:12 False hack-cclub lex, port 666
2005-11-04 01:45:21 False hitokiri lex, port 23485
2005-11-04 01:45:45 False hitokiri lex, port 1024
2005-11-04 01:45:46 True hack-cclub slaps lex around with a wet fish
大约有550万行数据,我正在尝试制作一些基本的可视化图表,比如对前25名昵称的时间排序等。我知道可以通过以下方式获取前25个昵称:
df['nick'].value_counts()[:25]
我想要的是像这样的滚动计数:
hack-cclub lex hitokiri
1 0 0
2 0 0
2 1 0
3 1 0
3 2 0
4 2 0
4 2 1
4 2 2
5 2 2
我希望能够绘制一个从一开始就显示前25个用户消息的面积图。我知道可以通过遍历整个数据框并保持计数来实现此目的,但由于这样做的整个重点是学习使用pandas,因此我希望有一种更符合惯用法的方法来实现它。同时,如果有相同的数据但是以排名而不是计数方式呈现,那就更好了:
hack-cclub lex hitokiri
1 2 2
1 2 2
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 2
1 2 2