【问题标题】:Python Pandas : Change rows and headersPython Pandas:更改行和标题
【发布时间】:2022-01-07 04:32:39
【问题描述】:

我有以下数组:

[['fic10', {'bulle_naif': '55'}, {'bulle_bool': '52'}, {'bulle_opt': '39'}, {'selection': '45'}, {'insertion': '20'}, {'rapide': '60'}], ['fic100', {'bulle_naif': '5050'}, {'bulle_bool': '5050'}, {'bulle_opt': '4816'}, {'selection': '4950'}, {'insertion': '2221'}, {'rapide': '6697'}], ['fic1000', {'bulle_naif': '2623195'}, {'bulle_bool': '1789209'}, {'bulle_opt': '2618499'}, {'selection': '2620905'}, {'insertion': '1535788'}, {'rapide': '1323294'}], ['fic10000', {'bulle_naif': '4764881010'}, {'bulle_bool': '926117379'}, {'bulle_opt': '4764749559'}, {'selection': '4764783390'}, {'insertion': '900955079'}, {'rapide': '506697139'}]]

然后我将其转换为数据框。

这是我将数组转换为数据框时得到的结果:

my output

但我想像这样格式化我的数据框:

desired output

谁能帮帮我?

【问题讨论】:

  • 我建议编辑您的问题以包含您将数组转换为数据框的代码。

标签: python arrays pandas dataframe sorting


【解决方案1】:

这是一段更简单易懂的代码,它可以帮助您解决问题。

import pandas as pd

array = [['fic10', {'bulle_naif': '55'}, {'bulle_bool': '52'}, {'bulle_opt': '39'}, {'selection': '45'}, {'insertion': '20'}, {'rapide': '60'}],
         ['fic100', {'bulle_naif': '5050'}, {'bulle_bool': '5050'}, {'bulle_opt': '4816'}, {'selection': '4950'}, {'insertion': '2221'}, {'rapide': '6697'}],
         ['fic1000', {'bulle_naif': '2623195'}, {'bulle_bool': '1789209'}, {'bulle_opt': '2618499'}, {'selection': '2620905'}, {'insertion': '1535788'}, {'rapide': '1323294'}],
         ['fic10000', {'bulle_naif': '4764881010'}, {'bulle_bool': '926117379'}, {'bulle_opt': '4764749559'}, {'selection': '4764783390'}, {'insertion': '900955079'}, {'rapide': '506697139'}]]

dt = pd.DataFrame()
for item in array:
    tmp = pd.DataFrame(index=[item[0]])
    for i in range(len(item)-1):
        if i != 0:
            for key, value in item[i].items():
                tmp[key] = value
    dt = dt.append(tmp)
print(dt)

输出:

          bulle_naif bulle_bool   bulle_opt   selection  insertion
fic10             55         52          39          45         20
fic100          5050       5050        4816        4950       2221
fic1000      2623195    1789209     2618499     2620905    1535788
fic10000  4764881010  926117379  4764749559  4764783390  900955079

【讨论】:

    【解决方案2】:

    让我们以pandas.DataFrame 更容易理解的方式呈现您的数据。

    第一种方法:每列一个条目 key:list 的字典

    此方法的目标是重新排列您的数据,使其看起来像一个字典,每列有一个条目,每列都有一个值列表:

    # {'bulle_naif': ['55', '5050', '2623195', '4764881010'],
    #  'bulle_bool': ['52', '5050', '1789209', '926117379'],
    #  'bulle_opt': ['39', '4816', '2618499', '4764749559'],
    #  'selection': ['45', '4950', '2620905', '4764783390'],
    #  'insertion': ['20', '2221', '1535788', '900955079'],
    #  'rapide': ['60', '6697', '1323294', '506697139'],
    #  'name': ['fic10', 'fic100', 'fic1000', 'fic10000']}
    

    这是进行该转换的代码:

    import pandas as pd
    
    raw_data = [['fic10', {'bulle_naif': '55'}, {'bulle_bool': '52'}, {'bulle_opt': '39'}, {'selection': '45'}, {'insertion': '20'}, {'rapide': '60'}], ['fic100', {'bulle_naif': '5050'}, {'bulle_bool': '5050'}, {'bulle_opt': '4816'}, {'selection': '4950'}, {'insertion': '2221'}, {'rapide': '6697'}], ['fic1000', {'bulle_naif': '2623195'}, {'bulle_bool': '1789209'}, {'bulle_opt': '2618499'}, {'selection': '2620905'}, {'insertion': '1535788'}, {'rapide': '1323294'}], ['fic10000', {'bulle_naif': '4764881010'}, {'bulle_bool': '926117379'}, {'bulle_opt': '4764749559'}, {'selection': '4764783390'}, {'insertion': '900955079'}, {'rapide': '506697139'}]]
    
    cleaned_data = { k: [] for d in raw_data[0][1:] for k in d.keys() }
    cleaned_data['name'] = []
    for row in raw_data:
        cleaned_data['name'].append(row[0])
        for d in row[1:]:
            for k,v in d.items():
                cleaned_data[k].append(v)
    
    print(cleaned_data)
    # {'bulle_naif': ['55', '5050', '2623195', '4764881010'],
    #  'bulle_bool': ['52', '5050', '1789209', '926117379'],
    #  'bulle_opt': ['39', '4816', '2618499', '4764749559'],
    #  'selection': ['45', '4950', '2620905', '4764783390'],
    #  'insertion': ['20', '2221', '1535788', '900955079'],
    #  'rapide': ['60', '6697', '1323294', '506697139'],
    #  'name': ['fic10', 'fic100', 'fic1000', 'fic10000']}
    
    
    # IMPORTANT NOTE
    # This cleaning-up is a bit careless
    # If a key is missing in one of the lists, the resulting data will be misaligned
    
    # Making sure data is not misaligned:
    assert(all(len(l) == len(cleaned_data['name']) for l in cleaned_data.values()))
    
    good_dataframe = pd.DataFrame(cleaned_data).set_index('name')
    print(good_dataframe)
    
    #           bulle_naif bulle_bool   bulle_opt   selection  insertion     rapide
    # name                                                                         
    # fic10             55         52          39          45         20         60
    # fic100          5050       5050        4816        4950       2221       6697
    # fic1000      2623195    1789209     2618499     2620905    1535788    1323294
    # fic10000  4764881010  926117379  4764749559  4764783390  900955079  506697139
    

    第二种方法:一个没有键但按顺序排列的二维数组

    如果您的数据已经排序,使得bulle_naifbulle_opt 等在每一行的顺序都相同,那么您可以去掉所有的字典并直接为pandas.DataFrame 提供一个二维数组:

    # assumes the rows of raw_data are all in the same order already
    array_data = [[row[0]] + [v for d in row[1:] for v in d.values()] for row in raw_data]
    
    print(array_data)
    # [['fic10', '55', '52', '39', '45', '20', '60'],
    #  ['fic100', '5050', '5050', '4816', '4950', '2221', '6697'],
    #  ['fic1000', '2623195', '1789209', '2618499', '2620905', '1535788', '1323294'],
    #  ['fic10000', '4764881010', '926117379', '4764749559', '4764783390', '900955079', '506697139']]
    
    keys = ['name'] + [k for d in raw_data[0][1:] for k in d.keys()]
    dataframe = pd.DataFrame(array_data, columns = keys).set_index('name')
    
    print(dataframe)
    #           bulle_naif bulle_bool   bulle_opt   selection  insertion     rapide
    # name                                                                         
    # fic10             55         52          39          45         20         60
    # fic100          5050       5050        4816        4950       2221       6697
    # fic1000      2623195    1789209     2618499     2620905    1535788    1323294
    # fic10000  4764881010  926117379  4764749559  4764783390  900955079  506697139
    

    如果您不知道所有行都已按相同顺序显示,则必须明确对它们进行排序以确保:

    # I shuffled the entries in raw_data
    raw_data = [
     ['fic10', {'bulle_bool': '52'}, {'bulle_naif': '55'}, {'selection': '45'}, {'insertion': '20'}, {'rapide': '60'}, {'bulle_opt': '39'}],
     ['fic100', {'bulle_opt': '4816'}, {'bulle_naif': '5050'}, {'insertion': '2221'}, {'selection': '4950'}, {'rapide': '6697'}, {'bulle_bool': '5050'}],
     ['fic1000', {'bulle_opt': '2618499'}, {'selection': '2620905'}, {'insertion': '1535788'}, {'bulle_bool': '1789209'}, {'rapide': '1323294'}, {'bulle_naif': '2623195'}],
     ['fic10000', {'selection': '4764783390'}, {'bulle_opt': '4764749559'}, {'bulle_bool': '926117379'}, {'bulle_naif': '4764881010'}, {'insertion': '900955079'}, {'rapide': '506697139'}]]
    
    array_data = [[row[0]] + [v for d in sorted(row[1:], key=lambda d: next(iter(d.keys()))) for v in d.values()] for row in raw_data]
    
    print(array_data)
    # [['fic10', '52', '55', '39', '20', '60', '45'],
    #  ['fic100', '5050', '5050', '4816', '2221', '6697', '4950'],
    #  ['fic1000', '1789209', '2623195', '2618499', '1535788', '1323294', '2620905'],
    #  ['fic10000', '926117379', '4764881010', '4764749559', '900955079', '506697139', '4764783390']]
    
    

    【讨论】:

    • 非常感谢您的回答!我可以将名称列作为我的数据框的索引吗?
    • @Y0yor 是的,使用.set_index。我编辑了我的答案。
    • @Y0yor 我用更简单的方法编辑了我的答案。
    【解决方案3】:

    创建数据框的问题在于数组的排序方式。我们需要重新排列数组以创建所需的数据框。

    这段代码为数据框创建了两个格式正确的列表:

    # create empty list for cols and rows
    row_names = []
    cols = []
    
    # loop over the array
    for i in arr:
        # add row name
        row_names.append((i[0]))
        # create dict for row values
        new_dict = {}
        # loop over row values
        for j in i[1:]:
            # add each value to new dict
            new_dict.update(j)
        # add row dict to columns
        cols.append(new_dict)
    

    运行时我们会得到

    row_names = ['fic10', 'fic100', 'fic1000', 'fic10000']
    cols = [
        {'bulle_naif': '55', 'bulle_bool': '52', 'bulle_opt': '39', 'selection': '45', 'insertion': '20', 'rapide': '60'}, 
        {'bulle_naif': '5050', 'bulle_bool': '5050', 'bulle_opt': '4816', 'selection': '4950', 'insertion': '2221', 'rapide': '6697'}, 
        {'bulle_naif': '2623195', 'bulle_bool': '1789209', 'bulle_opt': '2618499', 'selection': '2620905', 'insertion': '1535788', 'rapide': '1323294'}, 
        {'bulle_naif': '4764881010', 'bulle_bool': '926117379', 'bulle_opt': '4764749559', 'selection': '4764783390', 'insertion': '900955079', 'rapide': '506697139'}
    ]
    

    这可以很容易地转换为数据框:

    df = pd.DataFrame(cols, index=row_names)
    

    所以大家一起来:

    import pandas as pd
    
    
    arr = [['fic10', {'bulle_naif': '55'}, {'bulle_bool': '52'}, {'bulle_opt': '39'}, {'selection': '45'}, {'insertion': '20'}, {'rapide': '60'}], ['fic100', {'bulle_naif': '5050'}, {'bulle_bool': '5050'}, {'bulle_opt': '4816'}, {'selection': '4950'}, {'insertion': '2221'}, {'rapide': '6697'}], ['fic1000', {'bulle_naif': '2623195'}, {'bulle_bool': '1789209'}, {'bulle_opt': '2618499'}, {'selection': '2620905'}, {'insertion': '1535788'}, {'rapide': '1323294'}], ['fic10000', {'bulle_naif': '4764881010'}, {'bulle_bool': '926117379'}, {'bulle_opt': '4764749559'}, {'selection': '4764783390'}, {'insertion': '900955079'}, {'rapide': '506697139'}]]
    
    # create empty list for cols and rows
    row_names = []
    cols = []
    
    # loop over the array
    for i in arr:
        # add row name
        row_names.append((i[0]))
        # create dict for row values
        new_dict = {}
        # loop over row values
        for j in i[1:]:
            # add each value to new dict
            new_dict.update(j)
        # add row dict to columns
        cols.append(new_dict)
    # creta DataFrame
    df = pd.DataFrame(cols, index=row_names)
    

    输出:

              bulle_naif bulle_bool   bulle_opt   selection  insertion
    fic10             55         52          39          45         20
    fic100          5050       5050        4816        4950       2221
    fic1000      2623195    1789209     2618499     2620905    1535788
    fic10000  4764881010  926117379  4764749559  4764783390  900955079
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2017-03-31
      • 2015-09-28
      • 2017-04-21
      • 1970-01-01
      • 2021-10-20
      • 2012-06-10
      相关资源
      最近更新 更多