0

I am learning Pandas and am hitting a wall creating a dataFrame from the following input:

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())
    }

which creates:

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

my current DataFrame

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

which creates:

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

my desired output would expand the dicts for Owner, Resolution and Severity over columns or rows.

I have tried a number of SO/Web solutions including trying to flatten the dict which returns a AttributeError: 'int' object has no attribute 'split'.

Any help would be greatly appreciated.

0

1 Answer 1

1

You only need 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.