DataFrame Object

In [3]:
import pandas as pd

Constructing DataFrame

From a single Series object

In [7]:
#create prices Series Object:
prices_ds = pd.Series([1.5, 2, 2.5, 3],
            index=["apples", "oranges", "bananas", "strawberies"])
prices_ds
Out[7]:
apples         1.5
oranges        2.0
bananas        2.5
strawberies    3.0
dtype: float64
In [15]:
# create DataFrame Object from prices Series:
prices_df = pd.DataFrame(prices_ds, columns=["prices"])
prices_df
Out[15]:
prices
apples 1.5
oranges 2.0
bananas 2.5
strawberies 3.0

From a dictionary of Series objects

In [16]:
# the Series objects:
prices_ds = pd.Series([1.5, 2, 2.5, 3],
            index=["apples", "oranges", "bananas", "strawberries"])
suppliers_ds = pd.Series(["supplier1", "supplier2", "supplier4", "supplier3"],
            index=["apples", "oranges", "bananas", "strawberries"])
In [17]:
# the DataFrame object:
fruits_df = pd.DataFrame({
  "prices": prices_ds,
  "suppliers": suppliers_ds
})
print(fruits_df)
              prices  suppliers
apples           1.5  supplier1
oranges          2.0  supplier2
bananas          2.5  supplier4
strawberries     3.0  supplier3