熊猫从字典创建一个数据帧,其中包含 Numpy 数组



我正在学习熊猫,并且正在从以下输入创建数据帧:

tracked = {
'Created': (df1.count()['Created']),
'Closed': (df1.count()['Closed']),
'Owner': (df1['Owner'].value_counts().to_dict()),
'Resolution': (df1['Resolution'].value_counts().to_dict()),
'Severity': (df1['Severity'].value_counts().to_dict())
}

它创建:

pp.pprint(tracked)
{u'Closed': '11', #numpy
u'Created': '42', #numpy
u'Owner': {u'foo.bar': 3, #dict
u'FooBar': 6,
u'BarFoo': 30,
u'bar.foo': 3},
u'Resolution': {u'FalsePositive': 1, u'TruePositive': 10}, #dict
u'Severity': {1: 7, 2: 31, 3: 4}} #dict

我当前的数据帧

df4 = pd.DataFrame.from_dict({(i): tracked[i]
for i in tracked.keys()},
orient='index').transpose()

它创建:

pp.pprint(df4)
Owner                                  Resolution             Severity Closed Created
0  {u'foobar': 30, u'foo.bar': 3, u'bar.f...  {u'FalsePositive': 1, u'TruePositive': 10}  {1: 7, 2: 31, 3: 4}     11      42

我想要的输出将在列或行上扩展所有者、分辨率和严重性的字典。

我已经尝试了许多SO/Web解决方案,包括尝试扁平化返回AttributeError: 'int' object has no attribute 'split'的字典。

任何帮助将不胜感激。

你只需要pd.DataFrame

dict1={u'Closed': '11', #numpy
u'Created': '42', #numpy
u'Owner': {u'foo.bar': 3, #dict
u'FooBar': 6,
u'BarFoo': 30,
u'bar.foo': 3},
u'Resolution': {u'FalsePositive': 1, u'TruePositive': 10}, #dict
u'Severity': {1: 7, 2: 31, 3: 4}} #dict
print(dict1)

{'Closed': '11', 'Created': '42', 'Owner': {'foo.bar': 3, 'FooBar': 6, 'BarFoo': 30, 'bar.foo': 3}, 'Resolution': {'FalsePositive': 1, 'TruePositive': 10}, 'Severity': {1: 7, 2: 31, 3: 4}}

df=pd.DataFrame(dict1)
print(df)

Closed Created  Owner  Resolution  Severity
foo.bar           11      42    3.0         NaN       NaN
FooBar            11      42    6.0         NaN       NaN
BarFoo            11      42   30.0         NaN       NaN
bar.foo           11      42    3.0         NaN       NaN
FalsePositive     11      42    NaN         1.0       NaN
TruePositive      11      42    NaN        10.0       NaN
1                 11      42    NaN         NaN       7.0
2                 11      42    NaN         NaN      31.0
3                 11      42    NaN         NaN       4.0

相关内容

最新更新