1
+ """
2
+ 1148. Article Views I
3
+ Solved
4
+ Easy
5
+ Topics
6
+ Companies
7
+ SQL Schema
8
+ Pandas Schema
9
+
10
+ Table: Views
11
+
12
+ +---------------+---------+
13
+ | Column Name | Type |
14
+ +---------------+---------+
15
+ | article_id | int |
16
+ | author_id | int |
17
+ | viewer_id | int |
18
+ | view_date | date |
19
+ +---------------+---------+
20
+ There is no primary key (column with unique values) for this table, the table may have duplicate rows.
21
+ Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
22
+ Note that equal author_id and viewer_id indicate the same person.
23
+
24
+ Write a solution to find all the authors that viewed at least one of their own articles.
25
+
26
+ Return the result table sorted by id in ascending order.
27
+
28
+ The result format is in the following example.
29
+
30
+ Example 1:
31
+
32
+ Input:
33
+ Views table:
34
+ +------------+-----------+-----------+------------+
35
+ | article_id | author_id | viewer_id | view_date |
36
+ +------------+-----------+-----------+------------+
37
+ | 1 | 3 | 5 | 2019年08月01日 |
38
+ | 1 | 3 | 6 | 2019年08月02日 |
39
+ | 2 | 7 | 7 | 2019年08月01日 |
40
+ | 2 | 7 | 6 | 2019年08月02日 |
41
+ | 4 | 7 | 1 | 2019年07月22日 |
42
+ | 3 | 4 | 4 | 2019年07月21日 |
43
+ | 3 | 4 | 4 | 2019年07月21日 |
44
+ +------------+-----------+-----------+------------+
45
+ Output:
46
+ +------+
47
+ | id |
48
+ +------+
49
+ | 4 |
50
+ | 7 |
51
+ +------+
52
+ """
53
+
54
+ import pandas as pd
55
+
56
+ def article_views (views : pd .DataFrame ) -> pd .DataFrame :
57
+ unique_author_views = views [views ['author_id' ] == views ['viewer_id' ]].drop_duplicates ('author_id' )
58
+ ordered_author_views = unique_author_views .sort_values ('author_id' )
59
+ return ordered_author_views [['author_id' ]].rename (columns = {'author_id' : 'id' })
0 commit comments