updated March 30, 2021

Pandas DataFrame useful functions

Let's see how to create Pandas DataFrame from the Python collection data or other data frames.

1. Create from column values.

From dict by column:

import pandas as pd
df = pd.DataFrame({'col1': [1,2], 'col2': [3,4]})
df

Out:
   col1  col2
0     1     3
1     2     4

2. Create from rows.

From rows list where each row is list of values:

d = pd.DataFrame(
    [['a', 'b'], ['c', 'd']],
    columns=['col1', 'col2']
)
d

Out: 
  col1 col2
0    a    b
1    c    d

3. Combine 2 data frames with same columns into one.

Append rows of 2 data frames:

pd.concat([df, d])

Out: 
  col1 col2
0    1    3
1    2    4
0    a    b
1    c    d

4. Join data frames like SQL tables.

Merge two data frames by the column values:

products = pd.DataFrame(
    [['Pencil', 0.65], ['Notebook', 2.99]],
    columns=['Name', 'Price']
)
products

Out: 
       Name  Price
0    Pencil   0.65
1  Notebook   2.99
orders = pd.DataFrame([
    ['Pencil', 5],
    ['Pencil', 10],
    ['Notebook', 2],
    ['Pencil', 2],
    ['Notebook', 3]
  ],
  columns=['Name', 'Quantity']
)
orders
Out: 
       Name  Quantity
0    Pencil         5
1    Pencil        10
2  Notebook         2
3    Pencil         2
4  Notebook         3
df = pd.merge(orders, products, on='Name')
df

Out: 
       Name  Quantity  Price
0    Pencil         5   0.65
1    Pencil        10   0.65
2    Pencil         2   0.65
3  Notebook         2   2.99
4  Notebook         3   2.99

5. Make aggregation.

Aggregate by Name column calculating the Amount for each name:

df['Amount'] = df['Quantity'] * df['Price']  # add amount column
df.groupby(['Name']).aggregate({'Amount': 'sum'})

Out: 
          Amount
Name            
Notebook   14.95
Pencil     11.05