More on DataFrame Attributes:

  • Each of the three DataFrame components - the index, columns, and data - can be accessed from a DataFrame. DataFrames are ideal of managing heterogenous columns of data, NumPy arrays are better for homogenous data.
  • For example, we can use the DataFrame attributes index, columns, and values to assign the index, columns, and data to their own variables.
  • Note: the sheet_name parameter can be used to specify the sheet to read from an Excel file.
    • If sheet_name is not specified, the first sheet is read by default.
  • In addition the skiprows parameter can be used to skip rows at the beginning of the file.

Example of columns attribute:

python
1import pandas as pd
2
3pizza = pd.read_excel('./data/PizzaSheet.xlsx')
4columns = pizza.columns
5
6print(columns)

Output:

1Index(['Date', 'Size', 'Base', 'Topping #1', 'Topping #2', 'Topping #3',
2       'Topping #4', 'Topping #5', 'Price'],
3      dtype='object')

Example of index attribute:

python
1import pandas as pd
2
3pizza = pd.read_excel('./data/PizzaSheet.xlsx')
4index = pizza.index
5
6print(index)

Output:

1RangeIndex(start=0, stop=7, step=1)

  • This means the rows are labeled starting from 0 and incrementing by 1 up to (but not including) stop, which is 7 in this case.

Example of values attribute:

python
1import pandas as pd
2
3pizza = pd.read_excel('./data/PizzaSheet.xlsx')
4data = pizza.values # or pizza.to_numpy(); both are the same
5
6print(data)

Output:

1[[Timestamp('2018-02-20 00:00:00') 'L' 'Cheese' 'Pepperoni' 'Garlic' nan
2  nan nan 15.0]
3 [Timestamp('2018-02-20 00:00:00') 'M' 'Cheese' 'Peppers' 'Onions'
4  'Olives' nan nan 13.5]
5 [Timestamp('2018-02-20 00:00:00') 'S' 'Margherita' nan nan nan nan nan
6  10.0]
7 [Timestamp('2018-02-20 00:00:00') 'M' 'Margherita' nan nan nan nan nan
8  12.0]
9 [Timestamp('2018-02-21 00:00:00') 'L' 'Bianco' 'Spinach' nan nan nan nan
10  14.5]
11 [Timestamp('2018-02-21 00:00:00') 'M' 'Marinara' 'Capers' 'Anchovies'
12  'Pecorino' nan nan 13.5]
13 [Timestamp('2018-02-21 00:00:00') 'L' 'Cheese' 'Sausage' 'Pepperoni'
14  'Onions' 'Peppers' 'Mushrooms' 16.5]]