Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b72ca0

Browse files
Add file
1 parent b215e71 commit 3b72ca0

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

‎30_days_pandas/1148_article_views.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /