Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

merge_datasets.py 6.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  1. ##############################################################################################
  2. ################### Merging the datasets of publications related to COVID-19 #################
  3. ##############################################################################################
  4. ########################################################################
  5. # Importing the required libraries.
  6. import csv, re, pandas as pd, numpy as np
  7. from string import punctuation
  8. ########################################################################
  9. ########################################################################
  10. # 1. Defining the required functions
  11. ########################################################################
  12. # Defining the function "clean_title".
  13. def clean_title(title):
  14. if len(title) == 1 and title in punctuation:
  15. return None
  16. if title[0] in punctuation:
  17. title = title[1:]
  18. if title[-1] in punctuation:
  19. title = title[:-1]
  20. return re.sub(r"\s+", " ", title).lower()
  21. ########################################################################
  22. # 2. Getting and preprocessing the datasets
  23. ########################################################################
  24. ########################################################################
  25. # 2.1. arXiv
  26. ########################################################################
  27. # Getting the data.
  28. df_arxiv = pd.read_csv("data/prepared/arxiv_covid_19.csv", header=0,
  29. dtype={"id": "str"})
  30. # Changing the type of features.
  31. df_arxiv.loc[:, ["subject_areas", "authors"]] = df_arxiv.loc[:,
  32. ["subject_areas", "authors"]].apply(lambda x: x.apply(eval))
  33. df_arxiv.publication_date = pd.to_datetime(df_arxiv.publication_date)
  34. # Defining the feature "source".
  35. df_arxiv["source"] = "arXiv"
  36. # Normalizing the feature "title".
  37. df_arxiv.title = df_arxiv.title.apply(clean_title)
  38. ########################################################################
  39. # 2.2. bioRxiv
  40. ########################################################################
  41. # Getting the data.
  42. df_biorxiv = pd.read_csv("data/prepared/biorxiv_covid_19.csv", header=0,
  43. dtype={"id": "str"})
  44. # Changing the type of features.
  45. df_biorxiv.author_affil.loc[df_biorxiv.author_affil.notnull()] = df_biorxiv.author_affil.loc[
  46. df_biorxiv.author_affil.notnull()].apply(eval)
  47. df_biorxiv.publication_date = pd.to_datetime(df_biorxiv.publication_date)
  48. # Normalizing the feature "title".
  49. df_biorxiv.title = df_biorxiv.title.apply(clean_title)
  50. ########################################################################
  51. # 2.3. PubMed
  52. ########################################################################
  53. # Getting the data.
  54. df_pubmed = pd.read_csv("data/prepared/pubmed_covid_19.csv", header=0,
  55. dtype={"pubmed_id": "str"})
  56. # Changing the type of features.
  57. df_pubmed.auth_keywords.loc[df_pubmed.auth_keywords.notnull()] = df_pubmed.auth_keywords.loc[
  58. df_pubmed.auth_keywords.notnull()].apply(eval)
  59. df_pubmed.author_affil.loc[df_pubmed.author_affil.notnull()] = df_pubmed.author_affil.loc[
  60. df_pubmed.author_affil.notnull()].apply(eval)
  61. df_pubmed.publication_date = pd.to_datetime(df_pubmed.publication_date)
  62. # Defining the feature "source".
  63. df_pubmed["source"] = "PubMed"
  64. # Normalizing the feature "title".
  65. df_pubmed.title.loc[df_pubmed.title.notnull()] = df_pubmed.title.loc[
  66. df_pubmed.title.notnull()].apply(clean_title)
  67. ########################################################################
  68. # 2.4. Scopus
  69. ########################################################################
  70. # Getting the data.
  71. df_scopus = pd.read_csv("data/prepared/scopus_covid_19.csv", header=0,
  72. dtype={"id": "str", "eid": "str", "pii": "str", "pubmed_id": "str"})
  73. # Changing the type of features.
  74. df_scopus.auth_keywords.loc[df_scopus.auth_keywords.notnull()] = df_scopus.auth_keywords.loc[
  75. df_scopus.auth_keywords.notnull()].apply(eval)
  76. df_scopus.index_terms.loc[df_scopus.index_terms.notnull()] = df_scopus.index_terms.loc[
  77. df_scopus.index_terms.notnull()].apply(eval)
  78. df_scopus.affiliations.loc[df_scopus.affiliations.notnull()] = df_scopus.affiliations.loc[
  79. df_scopus.affiliations.notnull()].apply(eval)
  80. df_scopus.subject_areas.loc[df_scopus.subject_areas.notnull()] = df_scopus.subject_areas.loc[
  81. df_scopus.subject_areas.notnull()].apply(eval)
  82. df_scopus.authors.loc[df_scopus.authors.notnull()] = df_scopus.authors.loc[
  83. df_scopus.authors.notnull()].apply(eval)
  84. df_scopus.author_affil.loc[df_scopus.author_affil.notnull()] = df_scopus.author_affil.loc[
  85. df_scopus.author_affil.notnull()].apply(eval)
  86. df_scopus.references.loc[df_scopus.references.notnull()] = df_scopus.references.loc[
  87. df_scopus.references.notnull()].apply(eval)
  88. df_scopus.publication_date = pd.to_datetime(df_scopus.publication_date)
  89. # Defining the feature "source".
  90. df_scopus["source"] = "Scopus"
  91. # Normalizing the feature "title".
  92. df_scopus.title = df_scopus.title.apply(clean_title)
  93. ########################################################################
  94. # 3. Merging/Joining the datasets
  95. ########################################################################
  96. # Removing the duplicated records between arXiv and Scopus.
  97. df_arxiv = df_arxiv[~df_arxiv.title.isin(df_scopus.title)]
  98. # Removing the duplicated records between bioRxiv and Scopus.
  99. df_biorxiv = df_biorxiv[~df_biorxiv.title.isin(df_scopus.title)]
  100. # Removing the duplicated records between PubMed and Scopus.
  101. idx_removed = df_pubmed.pubmed_id[df_pubmed.pubmed_id.isin(df_scopus.pubmed_id) &
  102. df_pubmed.title.isin(df_scopus.title)].index.to_list()
  103. idx_removed += df_pubmed.pubmed_id[~df_pubmed.pubmed_id.isin(df_scopus.pubmed_id) &
  104. df_pubmed.title.isin(df_scopus.title)].index.to_list()
  105. idx_removed += df_pubmed.pubmed_id[df_pubmed.pubmed_id.isin(df_scopus.pubmed_id) &
  106. ~df_pubmed.title.isin(df_scopus.title)].index.to_list()
  107. df_pubmed = df_pubmed[~df_pubmed.index.isin(list(set(idx_removed)))]
  108. # Visualizing the final number of records for each dataset.
  109. print("arXiv:", df_arxiv.id.size)
  110. print("bioRxiv:", df_biorxiv.id.size)
  111. print("PubMed:", df_pubmed.pubmed_id.size)
  112. print("Scopus:", df_scopus.id.size)
  113. print("Expected total number of records for the final dataset:",
  114. (df_arxiv.id.size + df_biorxiv.id.size + df_pubmed.pubmed_id.size + df_scopus.id.size))
  115. # Merging/Joining the datasets.
  116. df_final = pd.concat([df_arxiv, df_biorxiv, df_pubmed, df_scopus], ignore_index=True)
  117. # Defining the "None" value for the "NaN" values.
  118. df_final.replace({np.nan: None}, inplace=True)
  119. # Renaming the feature "source".
  120. df_final.rename(columns={"source": "data_source"}, inplace=True)
  121. # Exporting the final dataset to CSV file.
  122. df_final.to_csv("data/raw/final_raw.csv", index=False, quoting=csv.QUOTE_ALL)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...