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 16 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  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. title = title.lower()
  17. title = title.replace("€", "").replace("…", "...").replace("τhe", "the").replace(
  18. "–", "-").replace("‘", "'").replace("“", "\"").replace("”", "\"").replace(
  19. "′", "'").replace("’", "'").replace("č", "c")
  20. while title[0] in punctuation or title[0] == " " or title[-1] in punctuation:
  21. if title[0] in punctuation:
  22. title = title[1:]
  23. if title[-1] in punctuation:
  24. title = title[:-1]
  25. title = title.strip()
  26. return re.sub(r"\"+", "", re.sub(r"\s+", " ", title))
  27. ########################################################################
  28. # 2. Getting and preprocessing the datasets
  29. ########################################################################
  30. ########################################################################
  31. # 2.1. arXiv
  32. ########################################################################
  33. # Getting the data.
  34. df_arxiv = pd.read_csv("data/prepared/arxiv_covid_19.csv", header=0,
  35. dtype={"id": "str"})
  36. # Defining the "None" value for the "NaN" values.
  37. df_arxiv.replace({np.nan: None}, inplace=True)
  38. # Changing the type of features.
  39. df_arxiv.loc[:, ["subject_areas", "authors"]] = df_arxiv.loc[:,
  40. ["subject_areas", "authors"]].apply(lambda x: x.apply(eval))
  41. df_arxiv.publication_date = pd.to_datetime(df_arxiv.publication_date)
  42. # Defining the feature "source".
  43. df_arxiv["source"] = "arXiv"
  44. # Normalizing the feature "title".
  45. df_arxiv.title = df_arxiv.title.apply(clean_title)
  46. ########################################################################
  47. # 2.2. bioRxiv
  48. ########################################################################
  49. # Getting the data.
  50. df_biorxiv = pd.read_csv("data/prepared/biorxiv_covid_19.csv", header=0,
  51. dtype={"id": "str"})
  52. # Defining the "None" value for the "NaN" values.
  53. df_biorxiv.replace({np.nan: None}, inplace=True)
  54. # Changing the type of features.
  55. df_biorxiv.author_affil.loc[df_biorxiv.author_affil.notnull()] = df_biorxiv.author_affil.loc[
  56. df_biorxiv.author_affil.notnull()].apply(eval)
  57. df_biorxiv.subject_areas.loc[df_biorxiv.subject_areas.notnull()] = df_biorxiv.subject_areas.loc[
  58. df_biorxiv.subject_areas.notnull()].apply(eval)
  59. df_biorxiv.publication_date = pd.to_datetime(df_biorxiv.publication_date)
  60. # Normalizing the feature "title".
  61. df_biorxiv.title = df_biorxiv.title.apply(clean_title)
  62. ########################################################################
  63. # 2.3. PubMed
  64. ########################################################################
  65. # Getting the data.
  66. df_pubmed = pd.read_csv("data/prepared/pubmed_covid_19.csv", header=0,
  67. dtype={"pubmed_id": "str"})
  68. # Defining the "None" value for the "NaN" values.
  69. df_pubmed.replace({np.nan: None}, inplace=True)
  70. # Changing the type of features.
  71. df_pubmed.auth_keywords.loc[df_pubmed.auth_keywords.notnull()] = df_pubmed.auth_keywords.loc[
  72. df_pubmed.auth_keywords.notnull()].apply(eval)
  73. df_pubmed.author_affil.loc[df_pubmed.author_affil.notnull()] = df_pubmed.author_affil.loc[
  74. df_pubmed.author_affil.notnull()].apply(eval)
  75. df_pubmed.publication_date = pd.to_datetime(df_pubmed.publication_date)
  76. # Defining the feature "source".
  77. df_pubmed["source"] = "PubMed"
  78. # Normalizing the feature "title".
  79. df_pubmed.title.loc[df_pubmed.title.notnull()] = df_pubmed.title.loc[
  80. df_pubmed.title.notnull()].apply(clean_title)
  81. ########################################################################
  82. # 2.4. Scopus
  83. ########################################################################
  84. # Getting the data.
  85. df_scopus = pd.read_csv("data/prepared/scopus_covid_19.csv", header=0, dtype=object)
  86. # Defining the "None" value for the "NaN" values.
  87. df_scopus.replace({np.nan: None}, inplace=True)
  88. # Changing the type of features.
  89. df_scopus.auth_keywords.loc[df_scopus.auth_keywords.notnull()] = df_scopus.auth_keywords.loc[
  90. df_scopus.auth_keywords.notnull()].apply(eval)
  91. df_scopus.index_terms.loc[df_scopus.index_terms.notnull()] = df_scopus.index_terms.loc[
  92. df_scopus.index_terms.notnull()].apply(eval)
  93. df_scopus.affiliations.loc[df_scopus.affiliations.notnull()] = df_scopus.affiliations.loc[
  94. df_scopus.affiliations.notnull()].apply(eval)
  95. df_scopus.subject_areas.loc[df_scopus.subject_areas.notnull()] = df_scopus.subject_areas.loc[
  96. df_scopus.subject_areas.notnull()].apply(eval)
  97. df_scopus.authors.loc[df_scopus.authors.notnull()] = df_scopus.authors.loc[
  98. df_scopus.authors.notnull()].apply(eval)
  99. df_scopus.author_affil.loc[df_scopus.author_affil.notnull()] = df_scopus.author_affil.loc[
  100. df_scopus.author_affil.notnull()].apply(eval)
  101. df_scopus.references.loc[df_scopus.references.notnull()] = df_scopus.references.loc[
  102. df_scopus.references.notnull()].apply(eval)
  103. df_scopus.publication_date = pd.to_datetime(df_scopus.publication_date)
  104. # Defining the feature "source".
  105. df_scopus["source"] = "Scopus"
  106. # Normalizing the feature "title".
  107. df_scopus.title = df_scopus.title.apply(clean_title)
  108. ########################################################################
  109. # 3. Merging/Joining the datasets
  110. ########################################################################
  111. # Filling the missing values of PubMed's features "title" and "doi" with data from Scopus.
  112. df_pubmed.loc[df_pubmed.pubmed_id.isin(df_scopus.pubmed_id.values) & df_pubmed.title.isnull(), "title"] = df_pubmed.pubmed_id[df_pubmed.pubmed_id.isin(df_scopus.pubmed_id.values) & df_pubmed.title.isnull()].apply(
  113. lambda x: df_scopus.title[df_scopus.pubmed_id == x].iloc[0])
  114. df_pubmed.loc[df_pubmed.pubmed_id.isin(df_scopus.pubmed_id.values) & df_pubmed.doi.isnull(), "doi"] = df_pubmed.pubmed_id[df_pubmed.pubmed_id.isin(df_scopus.pubmed_id.values) & df_pubmed.doi.isnull()].apply(
  115. lambda x: np.reshape(df_scopus.doi[df_scopus.pubmed_id == x].values, -1)[0] \
  116. if df_scopus.doi[df_scopus.pubmed_id == x].size > 0 else None)
  117. df_pubmed.loc[df_pubmed.doi[df_pubmed.doi.notnull()].isin(df_scopus.doi[df_scopus.doi.notnull()].values) &
  118. df_pubmed.title.isnull(), "title"] = df_pubmed.doi[df_pubmed.doi[df_pubmed.doi.notnull()].isin(
  119. df_scopus.doi[df_scopus.doi.notnull()].values) & df_pubmed.title.isnull()].apply(
  120. lambda x: df_scopus.title[df_scopus.doi == x].item())
  121. # Filling the missing values of PubMed's features "title", "abstract", "subject_areas" and "doi" with data from bioRxiv.
  122. df_pubmed.loc[df_pubmed.doi.isin(df_biorxiv.doi.values) & df_pubmed.title.isnull(), "title"] = df_pubmed.doi[df_pubmed.doi.isin(df_biorxiv.doi.values) & df_pubmed.title.isnull()].apply(
  123. lambda x: df_biorxiv.title[df_biorxiv.doi == x].item())
  124. df_pubmed.loc[df_pubmed.title.isin(df_biorxiv.title.values) & df_pubmed.doi.isnull(), "doi"] = df_pubmed.loc[df_pubmed.title.isin(df_biorxiv.title.values) & df_pubmed.doi.isnull(), ["doi", "title"]].apply(
  125. lambda x: df_biorxiv.doi[df_biorxiv.title == x.title].item() if not x.doi else x.doi, axis=1)
  126. df_pubmed.loc[df_pubmed.doi.isin(df_biorxiv.doi.values) & df_pubmed.abstract.isnull(), "abstract"] = df_pubmed.doi[df_pubmed.doi.isin(df_biorxiv.doi.values) & df_pubmed.abstract.isnull()].apply(
  127. lambda x: df_biorxiv.abstract[df_biorxiv.doi == x].item())
  128. df_pubmed.loc[df_pubmed.doi.isin(df_biorxiv.doi.values), "subject_areas"] = df_pubmed.doi[
  129. df_pubmed.doi.isin(df_biorxiv.doi.values)].apply(lambda x: df_biorxiv.subject_areas[
  130. df_biorxiv.doi == x].item())
  131. # Filling the missing values of PubMed's features "abstract" and "subject_areas" with data from arXiv.
  132. df_pubmed.loc[df_pubmed.title.isin(df_arxiv.title.values) & df_pubmed.abstract.isnull(), "abstract"] = df_pubmed.title[df_pubmed.title.isin(df_arxiv.title.values) & df_pubmed.abstract.isnull()].apply(
  133. lambda x: df_arxiv.abstract[df_arxiv.title == x].item())
  134. df_pubmed.loc[df_pubmed.title.isin(df_arxiv.title.values), "subject_areas"] = df_pubmed.title[
  135. df_pubmed.title.isin(df_arxiv.title.values)].apply(
  136. lambda x: df_arxiv.subject_areas[df_arxiv.title == x].item())
  137. # Filling the missing values of Scopus' features "abstract" and "subject_areas" with data from arXiv.
  138. df_scopus.loc[df_scopus.title.isin(df_arxiv.title.values) & df_scopus.abstract.isnull(), "abstract"] = df_scopus.title[df_scopus.title.isin(df_arxiv.title.values) & df_scopus.abstract.isnull()].apply(
  139. lambda x: df_arxiv.abstract[df_arxiv.title == x].item())
  140. df_scopus.loc[df_scopus.title.isin(df_arxiv.title.values) & df_scopus.subject_areas.isnull(),
  141. "subject_areas"] = df_scopus.title[df_scopus.title.isin(df_arxiv.title.values) &
  142. df_scopus.subject_areas.isnull()].apply(lambda x: df_arxiv.subject_areas[df_arxiv.title == x].item())
  143. # Filling the missing values of Scopus' features "doi" and "pubmed_id" with data from PubMed.
  144. df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) & df_scopus.doi.isnull(), "doi"] = df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) & df_scopus.doi.isnull(),
  145. ["doi", "pubmed_id"]].apply(lambda x: df_pubmed.doi[df_pubmed.pubmed_id == x.pubmed_id].item() \
  146. if not x.doi else x.doi, axis=1)
  147. df_scopus.loc[df_scopus.title.isin(df_pubmed.title[df_pubmed.title.notnull()].values)
  148. & df_scopus.pubmed_id.isnull() & df_scopus.doi[
  149. df_scopus.doi.notnull()].isin(df_pubmed.doi[df_pubmed.doi.notnull()].values), "pubmed_id"] = \
  150. df_scopus.loc[df_scopus.title.isin(df_pubmed.title[df_pubmed.title.notnull()].values)
  151. & df_scopus.pubmed_id.isnull() & df_scopus.doi[
  152. df_scopus.doi.notnull()].isin(df_pubmed.doi[df_pubmed.doi.notnull()].values),
  153. ["pubmed_id", "title", "doi"]].apply(lambda x: x.pubmed_id if x.pubmed_id else np.reshape(
  154. df_pubmed.pubmed_id[(df_pubmed.title == x.title) & (df_pubmed.doi == x.doi)].values, -1)[0] \
  155. if df_pubmed.pubmed_id[(df_pubmed.title == x.title) & (df_pubmed.doi == x.doi)].size > 0 \
  156. else None, axis=1)
  157. # Filling the missing values of Scopus' feature "abstract" with data from PubMed.
  158. df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) & df_scopus.abstract.isnull(), "abstract"] = df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) & df_scopus.abstract.isnull(),
  159. ["abstract", "pubmed_id"]].apply(lambda x: df_pubmed.abstract[
  160. df_pubmed.pubmed_id == x.pubmed_id].item() if not x.abstract else x.abstract, axis=1)
  161. df_scopus.loc[~df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) &
  162. df_scopus.title.isin(df_pubmed.title[df_pubmed.title.notnull()].values) & df_scopus.abstract.isnull() &
  163. df_scopus.doi[df_scopus.doi.notnull()].isin(df_pubmed.doi[df_pubmed.doi.notnull()].values), "abstract"] = \
  164. df_scopus.loc[~df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) &
  165. df_scopus.title.isin(df_pubmed.title[df_pubmed.title.notnull()].values) & df_scopus.abstract.isnull() &
  166. df_scopus.doi[df_scopus.doi.notnull()].isin(df_pubmed.doi[df_pubmed.doi.notnull()].values),
  167. ["abstract", "title", "doi"]].apply(lambda x: x.abstract if not x.abstract else np.reshape(
  168. df_pubmed.abstract[(df_pubmed.title == x.title) & (df_pubmed.doi == x.doi)].values, -1)[0] \
  169. if df_pubmed.abstract[(df_pubmed.title == x.title) & (df_pubmed.doi == x.doi)].size > 0 \
  170. else None, axis=1)
  171. # Filling the missing values of Scopus' feature "auth_keywords" with data from PubMed.
  172. df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) & df_scopus.auth_keywords.isnull(),
  173. "auth_keywords"] = df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) &
  174. df_scopus.auth_keywords.isnull(), ["auth_keywords", "pubmed_id"]].apply(
  175. lambda x: df_pubmed.auth_keywords[df_pubmed.pubmed_id == x.pubmed_id].item() \
  176. if not x.auth_keywords else x.auth_keywords, axis=1)
  177. df_scopus.loc[~df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) &
  178. df_scopus.title.isin(df_pubmed.title[df_pubmed.title.notnull()].values) & df_scopus.auth_keywords.isnull() &
  179. df_scopus.doi[df_scopus.doi.notnull()].isin(df_pubmed.doi[df_pubmed.doi.notnull()].values), "auth_keywords"] = \
  180. df_scopus.loc[~df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) &
  181. df_scopus.title.isin(df_pubmed.title[df_pubmed.title.notnull()].values) & df_scopus.auth_keywords.isnull() &
  182. df_scopus.doi[df_scopus.doi.notnull()].isin(df_pubmed.doi[df_pubmed.doi.notnull()].values),
  183. ["auth_keywords", "title", "doi"]].apply(lambda x: x.auth_keywords if x.auth_keywords else np.reshape(
  184. df_pubmed.auth_keywords[(df_pubmed.title == x.title) & (df_pubmed.doi == x.doi)].values, -1)[0] \
  185. if df_pubmed.auth_keywords[(df_pubmed.title == x.title) & (df_pubmed.doi == x.doi)].size > 0 \
  186. else None, axis=1)
  187. # Filling the missing values of Scopus' features "author_affil" and "subject_areas" with data from PubMed.
  188. df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) & df_scopus.author_affil.isnull(),
  189. "author_affil"] = df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) &
  190. df_scopus.author_affil.isnull(), ["author_affil", "pubmed_id"]].apply(
  191. lambda x: df_pubmed.author_affil[df_pubmed.pubmed_id == x.pubmed_id].item() \
  192. if not x.author_affil else x.author_affil, axis=1)
  193. df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) & df_scopus.subject_areas.isnull(),
  194. "subject_areas"] = df_scopus.loc[df_scopus.pubmed_id.isin(df_pubmed.pubmed_id.values) &
  195. df_scopus.subject_areas.isnull(), ["subject_areas", "pubmed_id"]].apply(
  196. lambda x: df_pubmed.subject_areas[df_pubmed.pubmed_id == x.pubmed_id].item() \
  197. if not x.subject_areas else x.subject_areas, axis=1)
  198. # Removing the duplicated records between arXiv and bioRxiv.
  199. df_arxiv = df_arxiv[~df_arxiv.title.isin(df_biorxiv.title.values)]
  200. # Removing the duplicated records between arXiv and PubMed.
  201. df_arxiv = df_arxiv[~df_arxiv.title.isin(df_pubmed.title[df_pubmed.title.notnull()].values)]
  202. # Removing the duplicated records between arXiv and Scopus.
  203. df_arxiv = df_arxiv[~df_arxiv.title.isin(df_scopus.title.values)]
  204. # Removing the duplicated records between bioRxiv and PubMed.
  205. df_biorxiv = df_biorxiv[~(df_biorxiv.title.isin(df_pubmed.title[df_pubmed.title.notnull()].values) &
  206. df_biorxiv.doi.isin(df_pubmed.doi[df_pubmed.doi.notnull()].values))]
  207. # Removing the duplicated records between bioRxiv and Scopus.
  208. df_biorxiv = df_biorxiv[~(df_biorxiv.title.isin(df_scopus.title.values) &
  209. df_biorxiv.doi.isin(df_scopus.doi[df_scopus.doi.notnull()].values))]
  210. # Removing the duplicated records between PubMed and Scopus.
  211. idx_removed = df_pubmed.pubmed_id[df_pubmed.pubmed_id.isin(df_scopus.pubmed_id[
  212. df_scopus.pubmed_id.notnull()].values)].index.to_list()
  213. idx_removed += df_pubmed.pubmed_id[~df_pubmed.pubmed_id.isin(df_scopus.pubmed_id[
  214. df_scopus.pubmed_id.notnull()].values) &
  215. df_pubmed.title.isin(df_scopus.title.values) &
  216. df_pubmed.doi.isin(df_scopus.doi[df_scopus.doi.notnull()].values)].index.to_list()
  217. df_pubmed = df_pubmed[~df_pubmed.index.isin(list(set(idx_removed)))]
  218. # Visualizing the final number of records for each dataset.
  219. print("arXiv:", df_arxiv.id.size)
  220. print("bioRxiv:", df_biorxiv.id.size)
  221. print("PubMed:", df_pubmed.pubmed_id.size)
  222. print("Scopus:", df_scopus.id.size)
  223. print("Expected total number of records for the final dataset:",
  224. (df_arxiv.id.size + df_biorxiv.id.size + df_pubmed.pubmed_id.size + df_scopus.id.size))
  225. # Merging/Joining the datasets.
  226. df_final = pd.concat([df_arxiv, df_biorxiv, df_pubmed, df_scopus], ignore_index=True)
  227. # Defining the "None" value for the "NaN" values.
  228. df_final.replace({np.nan: None}, inplace=True)
  229. # Renaming the feature "source".
  230. df_final.rename(columns={"source": "data_source"}, inplace=True)
  231. # Exporting the final dataset to CSV file.
  232. 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...