如何手动创建熊猫数据框

时间:2020-01-26 01:11:24

标签: python pandas

用最简单的术语来说,如何不使用字典,列表或数组来手动创建熊猫数据框。我可以创建列,但是由于某些原因我无法手动输入数据

这是创建的初始字典,但是我似乎可以访问和映射各个值:

python3.6.x

新功能

resource = {'<Invalid Value':{'None':{'None':'22'}},
        '<Not Defined>':{'None':{'None':'22'}},
        'Chipper':{'Chipper Required':{'EE_1410_0045':'13'}},
        'Climber':{'Climbers':{'EE_1410_0030':'22'}},
        'EWP 2 Man Crew':{'Medium EWP':{'EE_1410_0010':'2'}},
        'EWP 2 Man Large':{'Large EWP':{'EE_1410_0015':'3'}},
        'EWP 3 Man Crew':{'None':{'None':'22'}},
        'EWP 3 Man Large':{'None':{'None':'22'}},
        'Forestry Mulcher':{'Medium Mulcher':{'EE_1410_0070':'20'}},
        'Other - Add Note':{'None':{'None':'22'}},
        'Slasher':{'None':{'None':'22'}},
        'Slasher / Mulcher':{'None':{'None':'22'}},
        'Spray Crew':{'Spray Crew':{'EE_1410_0040':'18'}}}

1 个答案:

答案 0 :(得分:1)

我想你想要这个: (以后请发布带有给定数据和所需答案格式的问题, 没有数据,很难弄清楚OP想要得到什么。请避免XY问题。)

import numpy as np
import pandas as pd

resource = { '<Not Defined>':{'None':{'None':'22'}},
        'Chipper':{'Chipper Required':{'EE_1410_0045':'13'}},
        'Climber':{'Climbers':{'EE_1410_0030':'22'}},
        'EWP 2 Man Crew':{'Medium EWP':{'EE_1410_0010':'2'}},
        'EWP 2 Man Large':{'Large EWP':{'EE_1410_0015':'3'}},
        'EWP 3 Man Crew':{'None':{'None':'22'}},
        'EWP 3 Man Large':{'None':{'None':'22'}},
        'Forestry Mulcher':{'Medium Mulcher':{'EE_1410_0070':'20'}},
        'Other - Add Note':{'None':{'None':'22'}},
        'Slasher':{'None':{'None':'22'}},
        'Slasher / Mulcher':{'None':{'None':'22'}},
        'Spray Crew':{'Spray Crew':{'EE_1410_0040':'18'}}
        }

k = list(resource.keys())
v = list(resource.values())

index = k
column = [ list(vv.keys())[0] for vv in v]
value1 = [ list(list(vv.values())[0].keys())[0] for vv in v]
value2 = [ list(list(vv.values())[0].values())[0] for vv in v]

df = pd.DataFrame({'column': column, 'value1': value1,'value2': value2},index=index)

print(df)

输出

                             column        value1 value2
<Not Defined>                  None          None     22
Chipper            Chipper Required  EE_1410_0045     13
Climber                    Climbers  EE_1410_0030     22
EWP 2 Man Crew           Medium EWP  EE_1410_0010      2
EWP 2 Man Large           Large EWP  EE_1410_0015      3
EWP 3 Man Crew                 None          None     22
EWP 3 Man Large                None          None     22
Forestry Mulcher     Medium Mulcher  EE_1410_0070     20
Other - Add Note               None          None     22
Slasher                        None          None     22
Slasher / Mulcher              None          None     22
Spray Crew               Spray Crew  EE_1410_0040     18