Python-c03-DataFrame and Series

1
import pandas as pd
1
2
3
4
5
person = {
"first": "Corey",
"last": "Schafer",
"email": "CoreyMSchafer@gmail.com"
}
1
2
3
4
5
people = {
"first": ["Corey"],
"last": ["Schafer"],
"email": ["CoreyMSchafer@gmail.com"]
}
1
2
3
4
5
people = {
"first": ["Corey", 'Jane', 'John'],
"last": ["Schafer", 'Doe', 'Doe'],
"email": ["CoreyMSchafer@gmail.com", 'JaneDoe@email.com', 'JohnDoe@email.com']
}
1
people['email']

[‘CoreyMSchafer@gmail.com‘, ‘JaneDoe@email.com‘, ‘JohnDoe@email.com‘]

1
df = pd.DataFrame(people)
1
df

first last email
0 Corey Schafer CoreyMSchafer@gmail.com
1 Jane Doe JaneDoe@email.com
2 John Doe JohnDoe@email.com
1
df['email']

0 CoreyMSchafer@gmail.com
1 JaneDoe@email.com
2 JohnDoe@email.com
Name: email, dtype: object

1
2
#display multiple columns
df[['last', 'email']]

last email
0 Schafer CoreyMSchafer@gmail.com
1 Doe JaneDoe@email.com
2 Doe JohnDoe@email.com
1
2
3
# iloc: integer location: put the integer index to get the row
# get the first row in this example
df.iloc[0]

first Corey
last Schafer
email CoreyMSchafer@gmail.com
Name: 0, dtype: object

1
df.iloc[[0, 1]]

first last email
0 Corey Schafer CoreyMSchafer@gmail.com
1 Jane Doe JaneDoe@email.com
1
2
# get the email and last of the first and second row
df.loc[[0, 1], ['email', 'last']]

email last
0 CoreyMSchafer@gmail.com Schafer
1 JaneDoe@email.com Doe
1
df.iloc[[0, 1], ['email', 'last']]
---------------------------------------------------------------------------

IndexError Traceback (most recent call last)

Cell In [15], line 1
—-> 1 df.iloc[[0, 1], [‘email’, ‘last’]]

File /opt/conda/lib/python3.10/site-packages/pandas/core/indexing.py:1068, in _LocationIndexer.getitem(self, key)
1066 if self._is_scalar_access(key):
1067 return self.obj._get_value(*key, takeable=self._takeable)
-> 1068 return self._getitem_tuple(key)
1069 else:
1070 # we by definition only have the 0th axis
1071 axis = self.axis or 0

File /opt/conda/lib/python3.10/site-packages/pandas/core/indexing.py:1564, in _iLocIndexer._getitem_tuple(self, tup)
1562 def _getitem_tuple(self, tup: tuple):
-> 1564 tup = self._validate_tuple_indexer(tup)
1565 with suppress(IndexingError):
1566 return self._getitem_lowerdim(tup)

File /opt/conda/lib/python3.10/site-packages/pandas/core/indexing.py:874, in _LocationIndexer._validate_tuple_indexer(self, key)
872 for i, k in enumerate(key):
873 try:
–> 874 self._validate_key(k, i)
875 except ValueError as err:
876 raise ValueError(
877 “Location based indexing can only have “
878 f”[{self._valid_types}] types”
879 ) from err

File /opt/conda/lib/python3.10/site-packages/pandas/core/indexing.py:1478, in _iLocIndexer._validate_key(self, key, axis)
1476 # check that the key has a numeric dtype
1477 if not is_numeric_dtype(arr.dtype):
-> 1478 raise IndexError(f”.iloc requires numeric indexers, got {arr}”)
1480 # check that the key does not exceed the maximum size of the index
1481 if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):

IndexError: .iloc requires numeric indexers, got [‘email’ ‘last’]

1
df.iloc[[0, 1], [1,2]]

last email
0 Schafer CoreyMSchafer@gmail.com
1 Doe JaneDoe@email.com