【问题标题】:How to sort the list of text from json data to pandas?如何将文本列表从 json 数据排序到 pandas?
【发布时间】:2020-07-05 16:13:30
【问题描述】:

我已将 json 数据从单个文件夹转换为 pandas 数据框。但这份名单并没有按顺序列出。有人知道如何对数据进行排序吗?

这是 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']

但是,我的标签顺序如下: 标签

    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

有人知道如何根据标签对数据进行排序吗?谢谢

这是我的代码:

        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)

这是 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...

【问题讨论】:

  • 因为你有索引,我会假设索引映射到每个 json - 你可以使用 dataframe.sort_index()。我还建议,你使用 pathlib ...这样 json_files 可以写成: list(Path(path_to_json).rglob('*.json')) 只是一个建议。
  • 在哪里可以添加 dataframe.sort_index()
  • 我建议你分享你的数据框样本。更容易使用并提出更清晰的建议link 请发布数据而不是图片。只有几行你的数据框也显示了索引
  • 我在上面分享了我的数据框。@sammywemmy
  • 好的...你有标签栏吗?让我们以这种方式尝试...使用 pandas json :pandas.pydata.org/pandas-docs/stable/reference/api/… 获取您的数据。假设你有你的 json 文件。你可以写一个列表理解:[pd.read_json(file).assign(source = filename) for filename in list_filenames]。这是我们可以欣赏 pathlib 的地方。但试试看,让我们看看......

标签: python json pandas list


【解决方案1】:

您可以使用this 的解决方案,将FakeReal 字符串的拆分值降序排序,数字升序排序:

L = ['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']

class reversor:
    def __init__(self, obj):
        self.obj = obj

    def __eq__(self, other):
        return other.obj == self.obj

    def __lt__(self, other):
        return other.obj < self.obj

a = sorted(L, key=lambda x: (reversor(x.split('_')[1]), int(x.split('_')[2].split('-')[0])))
print (a)
['BuzzFeed_Real_1-Webpage.json', 'BuzzFeed_Real_2-Webpage.json',
 'BuzzFeed_Real_3-Webpage.json', 'BuzzFeed_Real_4-Webpage.json', 
 'BuzzFeed_Real_5-Webpage.json', 'BuzzFeed_Real_6-Webpage.json', 
 'BuzzFeed_Real_7-Webpage.json', 'BuzzFeed_Real_8-Webpage.json', 
 'BuzzFeed_Real_9-Webpage.json', 'BuzzFeed_Real_10-Webpage.json', 
 'BuzzFeed_Fake_1-Webpage.json', 'BuzzFeed_Fake_2-Webpage.json', 
 'BuzzFeed_Fake_3-Webpage.json', 'BuzzFeed_Fake_4-Webpage.json', 
 'BuzzFeed_Fake_5-Webpage.json', 'BuzzFeed_Fake_6-Webpage.json', 
 'BuzzFeed_Fake_7-Webpage.json', 'BuzzFeed_Fake_8-Webpage.json', 
 'BuzzFeed_Fake_9-Webpage.json', 'BuzzFeed_Fake_10-Webpage.json']

pandas 的另一个类似想法 - 将值拆分为新列,最后按 DataFrame.sort_values 排序:

df = pd.DataFrame({'a':L})
df = df.join(df['a'].str.split('_', expand=True))
df['num'] = df[2].str.extract('(\d+)', expand=False).astype(int)
df = df.sort_values([1, 'num'], ascending=[False, True])
print (df)
                                a         0     1                2  num
11   BuzzFeed_Real_1-Webpage.json  BuzzFeed  Real   1-Webpage.json    1
9    BuzzFeed_Real_2-Webpage.json  BuzzFeed  Real   2-Webpage.json    2
17   BuzzFeed_Real_3-Webpage.json  BuzzFeed  Real   3-Webpage.json    3
10   BuzzFeed_Real_4-Webpage.json  BuzzFeed  Real   4-Webpage.json    4
0    BuzzFeed_Real_5-Webpage.json  BuzzFeed  Real   5-Webpage.json    5
5    BuzzFeed_Real_6-Webpage.json  BuzzFeed  Real   6-Webpage.json    6
6    BuzzFeed_Real_7-Webpage.json  BuzzFeed  Real   7-Webpage.json    7
7    BuzzFeed_Real_8-Webpage.json  BuzzFeed  Real   8-Webpage.json    8
8    BuzzFeed_Real_9-Webpage.json  BuzzFeed  Real   9-Webpage.json    9
12  BuzzFeed_Real_10-Webpage.json  BuzzFeed  Real  10-Webpage.json   10
15   BuzzFeed_Fake_1-Webpage.json  BuzzFeed  Fake   1-Webpage.json    1
16   BuzzFeed_Fake_2-Webpage.json  BuzzFeed  Fake   2-Webpage.json    2
18   BuzzFeed_Fake_3-Webpage.json  BuzzFeed  Fake   3-Webpage.json    3
13   BuzzFeed_Fake_4-Webpage.json  BuzzFeed  Fake   4-Webpage.json    4
3    BuzzFeed_Fake_5-Webpage.json  BuzzFeed  Fake   5-Webpage.json    5
2    BuzzFeed_Fake_6-Webpage.json  BuzzFeed  Fake   6-Webpage.json    6
19   BuzzFeed_Fake_7-Webpage.json  BuzzFeed  Fake   7-Webpage.json    7
4    BuzzFeed_Fake_8-Webpage.json  BuzzFeed  Fake   8-Webpage.json    8
1    BuzzFeed_Fake_9-Webpage.json  BuzzFeed  Fake   9-Webpage.json    9
14  BuzzFeed_Fake_10-Webpage.json  BuzzFeed  Fake  10-Webpage.json   10

a = df['a'].tolist()
print (a)
['BuzzFeed_Real_1-Webpage.json', 'BuzzFeed_Real_2-Webpage.json',
 'BuzzFeed_Real_3-Webpage.json', 'BuzzFeed_Real_4-Webpage.json', 
 'BuzzFeed_Real_5-Webpage.json', 'BuzzFeed_Real_6-Webpage.json', 
 'BuzzFeed_Real_7-Webpage.json', 'BuzzFeed_Real_8-Webpage.json', 
 'BuzzFeed_Real_9-Webpage.json', 'BuzzFeed_Real_10-Webpage.json', 
 'BuzzFeed_Fake_1-Webpage.json', 'BuzzFeed_Fake_2-Webpage.json', 
 'BuzzFeed_Fake_3-Webpage.json', 'BuzzFeed_Fake_4-Webpage.json', 
 'BuzzFeed_Fake_5-Webpage.json', 'BuzzFeed_Fake_6-Webpage.json', 
 'BuzzFeed_Fake_7-Webpage.json', 'BuzzFeed_Fake_8-Webpage.json', 
 'BuzzFeed_Fake_9-Webpage.json', 'BuzzFeed_Fake_10-Webpage.json']

【讨论】:

    【解决方案2】:

    这应该为您提供从文件名创建索引所需的内容。让我知道您是否需要设置索引的帮助,以及是否需要双索引或合并为单个索引:

    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]
    
    # Add column to your data frame containing 'json_files' list values
    jsons_data['json_files'] = json_files
    
    import re
    
    # Create Regex to identify 'Fake' or 'Real' BuzzFeed
    news_type = r"(Fake|Real)"
    
    # Create Regex to extract numeric count
    news_type_count = r"(\d+)"
    
    # Extract new type to column
    jsons_data['news_type'] = jsons_data['json_files'].str.extract(pat=news_type)
    
    # Extract numeric count to columne
    jsons_data['news_type_count'] = jsons_data['json_files'].str.extract(pat=news_type_count)
    
    # Convert numeric count to integer
    jsons_data['news_type_count'] = jsons_data['news_type_count'].astype(int)
    
    # Sort dataframe by 'news_type' and 'news_type_count'
    jsons_data = jsons_data.sort_values(by=['news_type', 'news_type_count'])
    
    # Print head of dataframe
    print(jsons_data.head())
    

    【讨论】:

    • 非常感谢您的帮助!我先试试! :)
    猜你喜欢
    • 2020-11-05
    • 2020-07-10
    • 1970-01-01
    • 2023-02-09
    • 2021-07-20
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2021-04-28
    相关资源
    最近更新 更多