I had converted json data from single folder to pandas dataframe. But the list didn't come out sequentially. Does anybody know how to sort the data?
This is output of json_files:
['BuzzFeed_Real_5-Webpage.json',
'BuzzFeed_Fake_9-Webpage.json',
'BuzzFeed_Fake_6-Webpage.json',
'BuzzFeed_Fake_5-Webpage.json',
'BuzzFeed_Fake_8-Webpage.json',
'BuzzFeed_Real_6-Webpage.json',
'BuzzFeed_Real_7-Webpage.json',
'BuzzFeed_Real_8-Webpage.json',
'BuzzFeed_Real_9-Webpage.json',
'BuzzFeed_Real_2-Webpage.json',
'BuzzFeed_Real_4-Webpage.json',
'BuzzFeed_Real_1-Webpage.json',
'BuzzFeed_Real_10-Webpage.json',
'BuzzFeed_Fake_4-Webpage.json',
'BuzzFeed_Fake_10-Webpage.json',
'BuzzFeed_Fake_1-Webpage.json',
'BuzzFeed_Fake_2-Webpage.json',
'BuzzFeed_Real_3-Webpage.json',
'BuzzFeed_Fake_3-Webpage.json',
'BuzzFeed_Fake_7-Webpage.json']
However, my label is sequential as follows: Label
label
0 BuzzFeed_Real_1
1 BuzzFeed_Real_2
2 BuzzFeed_Real_3
3 BuzzFeed_Real_4
4 BuzzFeed_Real_5
5 BuzzFeed_Real_6
6 BuzzFeed_Real_7
7 BuzzFeed_Real_8
8 BuzzFeed_Real_9
9 BuzzFeed_Real_10
10 BuzzFeed_Fake_1
11 BuzzFeed_Fake_2
12 BuzzFeed_Fake_3
13 BuzzFeed_Fake_4
14 BuzzFeed_Fake_5
15 BuzzFeed_Fake_6
16 BuzzFeed_Fake_7
17 BuzzFeed_Fake_8
18 BuzzFeed_Fake_9
19 BuzzFeed_Fake_10
Does anybody know how to sort the data based on the label? Thank you
Here is my code:
import os, json
import pandas as pd
import numpy as np
path_to_json = 'data/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('json')]
print(json_files)
#Here I define my pandas dataframe with the colums I want to get from json
jsons_data = pd.DataFrame(columns=['text','title'])
#We need both json and an index number so use enumerate()
for index, js in enumerate(json_files):
with open(os.path.join(path_to_json,js)) as json_file:
json_text = json.load(json_file)
#the same structure
text = json_text['text']
title = json_text['title']
#Here I push a list of data into pandas DataFrame at row given by 'index'
jsons_data.loc[index] = [text,title]
#Now that we have the pertinen json data in our DataFrame
print(jsons_data)
and this is output of jsons_data:
text title
0 Story highlights Obams reaffirms US commitment... Obama in NYC: 'We all have a role to play' in ...
1 Well THAT’S Weird. If the Birther movement is ... The AP, In 2004, Said Your Boy Obama Was BORN ...
2 The man arrested Monday in connection with the... Bombing Suspect Filed Anti-Muslim Discriminati...
3 The Haitians in the audience have some newswor... 'Reporters' FLEE When Clintons Get EXPOSED!
4 Chicago Environmentalist Scumbags\n\nLeftists ... The Black Sphere with Kevin Jackson
5 Obama weighs in on the debate\n\nPresident Bar... Obama weighs in on the debate
6 Story highlights Ted Cruz refused to endorse T... Donald Trump's rise puts Ted Cruz in a bind
7 Last week I wrote an article titled “Donald Tr... More Milestone Moments for Donald Trump! – Eag...
8 Story highlights Trump has 45%, Clinton 42% an... Georgia poll: Donald Trump, Hillary Clinton in...
9 Story highlights "This, though, is certain: to... Hillary Clinton on police shootings: 'too many...
10 McCain Criticized Trump for Arpaio’s Pardon… S... NFL Superstar Unleashes 4 Word Bombshell on Re...
11 On Saturday, September 17 at 8:30 pm EST, an e... Another Terrorist Attack in NYC…Why Are we STI...
12 Less than a day after protests over the police... Donald Trump: Drugs a 'Very, Very Big Factor' ...
13 Dolly Kyle has written a scathing “tell all” b... HILLARY ON DISABLED CHILDREN During Easter Egg...
14 Former President Bill Clinton and his Clinton ... Charity: Clinton Foundation Distributed “Water...
15 I woke up this morning to find a variation of ... Proof The Mainstream Media Is Manipulating The...
16 Thanks in part to the declassification of Defe... Declassified Docs Show That Obama Admin Create...
17 Critical Counties is a CNN series exploring 11... Critical counties: Wake County, NC, could put ...
18 The Democrats are using an intimidation tactic... Why is it “RACIST” to Question Someone’s Birth...
19 Back when the news first broke about the pay-t... Clinton Foundation Spent 5.7% on Charity; Rest...