我有一个数据集,其中包含每篇文章的观看次数。
我正在尝试为每个故事计算每天的额外观看次数,以便可以对其进行绘图。
我只能做到一个故事。
storyviews = stats[['title', 'views']].sort_values(by=['title','views'])
storyviews = stats[stats["title"] == "Getting Started with TDD"]
storyviews = storyviews[["title","views"]].sort_values(by=['title','views'])
difference = storyviews.set_index('title').diff()
difference = difference.dropna(subset=['views'])
difference
我得到正确的结果。
是否有一种方法可以一次完成所有故事?
数据集
y,m,d,mediumID,title,link,publication,mins,views,reads,readRatio,fans,pubDate,liveDate
2020,06,30,a1777d8bf7e,Swift — Filtering: A Real Example,https://levelup.gitconnected.com/swift-filtering-a-real-example-a1777d8bf7e,Level Up Coding,4 min read,35,13,37.142857142857146,1,2020-06-17,2020-06-26
2020,06,30,6f5fc68b0b43,SwiftUI 2: an overview,https://levelup.gitconnected.com/swiftui-2-an-overview-6f5fc68b0b43,Level Up Coding,3 min read,43,22,51.16279069767442,2,2020-06-24,2020-06-24
2020,07,01,a1777d8bf7e,Swift — Filtering: A Real Example,https://levelup.gitconnected.com/swift-filtering-a-real-example-a1777d8bf7e,Level Up Coding,4 min read,37,13,35.13513513513514,1,2020-06-17,2020-06-26
2020,07,01,6f5fc68b0b43,SwiftUI 2: an overview,https://levelup.gitconnected.com/swiftui-2-an-overview-6f5fc68b0b43,Level Up Coding,3 min read,57,29,50.87719298245614,10,2020-06-24,2020-06-24
2020,07,02,a1777d8bf7e,Swift — Filtering: A Real Example,https://levelup.gitconnected.com/swift-filtering-a-real-example-a1777d8bf7e,Level Up Coding,4 min read,37,13,35.13513513513514,1,2020-06-17,2020-06-26
2020,07,02,6f5fc68b0b43,SwiftUI 2: an overview,https://levelup.gitconnected.com/swiftui-2-an-overview-6f5fc68b0b43,Level Up Coding,3 min read,76,43,56.578947368421055,15,2020-06-24,2020-06-24
2020,07,03,a1777d8bf7e,Swift — Filtering: A Real Example,https://levelup.gitconnected.com/swift-filtering-a-real-example-a1777d8bf7e,Level Up Coding,4 min read,40,13,34.21052631578947,1,2020-06-17,2020-06-26
2020,07,03,6f5fc68b0b43,SwiftUI 2: an overview,https://levelup.gitconnected.com/swiftui-2-an-overview-6f5fc68b0b43,Level Up Coding,3 min read,152,70,46.05263157894737,20,2020-06-24,2020-06-24
谢谢, 尼古拉斯
答案 0 :(得分:2)
你可以试一下吗?
cols = ['title', 'views']
storyviews = stats[cols].sort_values(by=cols)
res = storyviews.set_index('title').groupby('title', sort=False).diff().dropna()
输出:
views
title
SwiftUI 2: an overview 14.0
SwiftUI 2: an overview 19.0
SwiftUI 2: an overview 76.0
Swift — Filtering: A Real Example 2.0
Swift — Filtering: A Real Example 0.0
Swift — Filtering: A Real Example 3.0
要绘制图例,标题...,您可能要问另一个问题。我没有答案。要使您开始进行该图,请尝试此操作。
res.reset_index().groupby('title', sort=False).plot()
答案 1 :(得分:0)
因此,您只是想获取每个标题的所有观看次数的总和?这是从熊猫DataFrame使用字典转换的一种方式:
...
dict = df.groupby('title').groups
for key in dict:
numpyarray_diff_between = np.diff(np.array(list(dict[key])))
...