Lesson 6: Data Conversion Recipes
Pragmatic AI Labs
This notebook was produced by Pragmatic AI Labs. You can continue learning about these topics by:
- Buying a copy of Pragmatic AI: An Introduction to Cloud-Based Machine Learning
- Reading an online copy of Pragmatic AI:Pragmatic AI: An Introduction to Cloud-Based Machine Learning
- Watching video Essential Machine Learning and AI with Python and Jupyter Notebook-Video-SafariOnline on Safari Books Online.
- Watching video AWS Certified Machine Learning-Speciality
- Purchasing video Essential Machine Learning and AI with Python and Jupyter Notebook- Purchase Video
- Viewing more content at noahgift.com
6.1 Convert lists to dicts, and dicts to lists
Converting Lists to Dictionaries
Create basic dictionary
key_values = [('one', 1), ('two', 2), ('three', 3)]
d = dict( key_values )
d
{'one': 1, 'three': 3, 'two': 2}
Zip two lists
instruments = [ 'violin', 'lute', 'banjo', 'accordian']
players = [ 'Anne-Sophie Mutter', 'Julian Bream', 'Noam Pikelny', 'Astor Pantaleón Piazzolla']
d = dict(zip(instruments, players))
d
From keys
racers = ['Tom', 'Bill', 'Will', 'Jill']
start_distance = 0
d = dict.fromkeys(racers, start_distance)
d
Converting Dictionaries to Lists
d = {'name': 'toby', 'id' : 14}
Get a list of keys
list(d)
Get keys in sorted order
sorted(d)
Get list of values
list(d.values())
6.2 Convert dicts to pandas Dataframe
Create DataFrame using data parameter
from pandas import DataFrame
d = {'first': ['Jill', 'Solma', 'Elizabeth'], 'last': ['Stein', 'Smith', 'Tudor']}
DataFrame(data=d)
first | last | |
---|---|---|
0 | Jill | Stein |
1 | Solma | Smith |
2 | Elizabeth | Tudor |
Use class from_dict method
DataFrame.from_dict(d)
first | last | |
---|---|---|
0 | Jill | Stein |
1 | Solma | Smith |
2 | Elizabeth | Tudor |
Create DataFrame with index orientation
d = {0: ['Edward', 'Tudor'], 1: ['Robert', 'Redford'], 3: ['Earl', 'Scruggs']}
df = DataFrame.from_dict(d, orient='index')
df.columns=['first', 'last']
df
first | last | |
---|---|---|
0 | Edward | Tudor |
1 | Robert | Redford |
3 | Earl | Scruggs |
Assign column names
d = {'a': 'A', 'b': 'B', 'c': 'C'}
df = DataFrame(list(d.items()), columns=['lower', 'upper'])
df
lower | upper | |
---|---|---|
0 | a | A |
1 | b | B |
2 | c | C |
6.3 Convert characters to integers and back
Cast str to int
Base 10
int('011')
11
Base 2
int('011', 2)
3
Base 6
int('011', 6)
7
Base 8
int('011', 8)
9
Base 16
int('011', 16)
17
Cast int to string
one = str(1)
type(one)
str
6.4 Convert between hexadecimal, binary, and floats
Cast to str from float
a_str = str(12.4)
f" {a_str!r} is a {type(a_str)}"
" '12.4' is a <class 'str'>"
Cast to float from str
a_str = "12.3"
a_float = float(a_str)
f" {a_float!r} is a {type(a_float)}"
" 12.3 is a <class 'float'>"
Hexadecimal
Int to hex str
int_hex = hex(18)
f" hex(18) returns the {type(int_hex)}: {int_hex!r}"
" hex(18) returns the <class 'str'>: '0x12'"
Float to hex str
float_hex = 12.0.hex()
f" 12.4.hex() returns the {type(float_hex)}: {float_hex!r}"
" 12.4.hex() returns the <class 'str'>: '0x1.8000000000000p+3'"
Conversion to and from binary
Bytes literal
Similar to strings, but limited to ASCII characters.
bytes_str = b"some bytes literal"
type(bytes_str)
bytes
Encode to bytes
import base64
bytes_str = b"Encode this string"
encoded_str = base64.b64encode(bytes_str)
f"The encoded string {encoded_str!r} is of type {type(encoded_str)}"
"The encoded string b'RW5jb2RlIHRoaXMgc3RyaW5n' is of type <class 'bytes'>"
Decode from bytes
base64.b64decode(encoded_str)
b'Encode this string'
Notes
- Dictionaries
- More on lists
- Dataframe.from_dict
- https://stackoverflow.com/questions/4576115/convert-a-list-to-a-dictionary-in-python
- https://thispointer.com/python-how-to-convert-a-list-to-dictionary/