Last active
July 1, 2025 06:18
-
-
Save davidlenz/deff6cc7405d58efa32f4dfe12a6db8b to your computer and use it in GitHub Desktop.
20 newsgroup dataset from sklearn to csv.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.datasets import fetch_20newsgroups | |
| import pandas as pd | |
| def twenty_newsgroup_to_csv(): | |
| newsgroups_train = fetch_20newsgroups(subset='train', remove=('headers', 'footers', 'quotes')) | |
| df = pd.DataFrame([newsgroups_train.data, newsgroups_train.target.tolist()]).T | |
| df.columns = ['text', 'target'] | |
| targets = pd.DataFrame( newsgroups_train.target_names) | |
| targets.columns=['title'] | |
| out = pd.merge(df, targets, left_on='target', right_index=True) | |
| out['date'] = pd.to_datetime('now') | |
| out.to_csv('20_newsgroup.csv') | |
| twenty_newsgroup_to_csv() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @sufyanafzal7 and @nelvintan, thanks for telling me, just added the dataset here if you are interested:
https://github.com/JumpingDino/nlp-datasets/tree/main/data