Nachengue
Nachengue

Reputation: 385

Transform a pandas serie of collections.counter objects into a many columns of pandas dataframe

i have a serie of collection.counter objects in each row. Like this:

0       {'PRP$': 1, 'NNS': 1, 'VBP': 1, 'DT': 3, 'NN':...
1                   {'JJS': 1, 'NN': 4, 'IN': 1, 'JJ': 1}
2       {'DT': 2, 'NNS': 3, 'VBD': 1, 'TO': 1, 'VB': 1...
3          {'CD': 2, 'NNS': 3, 'JJ': 1, 'NN': 2, 'IN': 1}
4       {'RB': 1, 'VBN': 1, 'VBD': 1, 'DT': 2, 'NN': 4...
                              ...                        
7602    {'DT': 2, 'NN': 4, 'RB': 2, 'VBD': 1, 'CC': 1,...
7603    {'NNS': 2, 'VBP': 1, 'DT': 3, 'NN': 6, 'VBZ': ...
7604    {'NNS': 5, 'VBN': 2, 'IN': 1, 'NN': 4, 'JJ': 1...
7605    {'IN': 4, 'DT': 2, 'JJ': 2, 'NN': 5, 'VBP': 2,...
7606       {'JJ': 2, 'NN': 8, 'NNS': 2, 'CD': 1, 'IN': 2}

And i want to create new columns in my pandas dataframe with something like this:

re-arranged_data = {'PRP$':[1,2,3...1,0],
            'NN':[1,1,1,0,...1],
            'DT':[0,0,0,0,1000],
             ...
           }

I don't know how to make this step and also don't know how to add the re-arranged_data as new columns.

Any help?

Upvotes: 2

Views: 179

Answers (2)

Gilseung Ahn
Gilseung Ahn

Reputation: 2624

Use df.from_records().

Example code is here.

import pandas as pd
from collections import Counter
a = Counter({'PRP$':1, 'NNS':1})
b = Counter({'JJS':2, 'NNS': 3})

A = pd.Series([a, b])

pd.DataFrame.from_records(A, columns = A.sum().keys())

Upvotes: 4

Vinay Kashyap
Vinay Kashyap

Reputation: 101

You can actually append it into the dataframe import pandas as pd import collections

dataframe = pd.DataFrame(columns=["PRP", "NN" ....]) dataframe.append(collection.counter)

Upvotes: 1

Related Questions