bugcy013
3/18/2018 - 11:34 PM

Different ways creating DataFrame in Pandas

Different ways creating DataFrame in Pandas

#!/usr/bin/env python

import pandas as pd
from collections import OrderedDict

'''
## Row Oriented
'''
sales = [
    {'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': 140},
    {'account': 'Alpha Co',  'Jan': 200, 'Feb': 210, 'Mar': 215},
    {'account': 'Blue Inc',  'Jan': 50,  'Feb': 90,  'Mar': 95}
]
df1 = pd.DataFrame(sales)

sales = [
    ('Jones LLC', 150, 200, 50),
    ('Alpha Co', 200, 210, 90),
    ('Blue Inc', 140, 215, 95)
]
labels = ['account', 'Jan', 'Feb', 'Mar']
df2 = pd.DataFrame.from_records(sales, columns=labels)

"""
## Column Oriented
"""

sales = {
    'account': ['Jones LLC', 'Alpha Co', 'Blue Inc'],
    'Jan': [150, 200, 50],
    'Feb': [200, 210, 90],
    'Mar': [140, 215, 95]
}
df3 = pd.DataFrame.from_dict(sales)

## When using a dictionary, columns order in note preserved. Explicity order them:
## df = df [['account', 'Jan', 'Feb', 'Mar']]

sales = OrderedDict(
    [
        ('account', ['Jones LLC', 'Alpha Co', 'Blue Inc']),
        ('Jan', [150, 200, 50]),
        ('Feb',  [200, 210, 90]),
        ('Mar', [140, 215, 95])
    ]
)
df3_1 = pd.DataFrame.from_dict(sales)

sales = [
    ('account', ['Jones LLC', 'Alpha Co', 'Blue Inc']),
    ('Jan', [150, 200, 50]),
    ('Feb', [200, 210, 90]),
    ('Mar', [140, 215, 95]),
]
df4 = pd.DataFrame.from_items(sales)