3.8. Read Python

  • pd.DataFrame()

3.8.1. SetUp

>>> import pandas as pd

3.8.2. Dict of List

>>> data = {
...     'firstname': ['Mark', 'Melissa', 'Rick'],
...     'lastname': ['Watney', 'Lewis', 'Martinez'],
...     'role': ['botanist', 'commander', 'pilot'],
... }
>>>
>>> df = pd.DataFrame(data)
>>> df
  firstname  lastname       role
0      Mark    Watney   botanist
1   Melissa     Lewis  commander
2      Rick  Martinez      pilot

3.8.3. List of Dict

>>> data = [
...     {'firstname': 'Mark', 'lastname': 'Watney', 'role': 'botanist'},
...     {'firstname': 'Melissa', 'lastname': 'Lewis', 'role': 'commander'},
...     {'firstname': 'Rick', 'lastname': 'Martinez', 'role': 'pilot'},
... ]
>>>
>>> df = pd.DataFrame(data)
>>> df
  firstname  lastname       role
0      Mark    Watney   botanist
1   Melissa     Lewis  commander
2      Rick  Martinez      pilot

3.8.4. List of Tuple

>>> data = [
...     ('Mark', 'Watney', 'botanist'),
...     ('Melissa', 'Lewis', 'commander'),
...     ('Rick', 'Martinez', 'pilot'),
... ]
>>>
>>> df = pd.DataFrame(data, columns=['firstname', 'lastname', 'role'])
>>> df
  firstname  lastname       role
0      Mark    Watney   botanist
1   Melissa     Lewis  commander
2      Rick  Martinez      pilot

3.8.5. Assignments

# %% About
# - Name: Pandas ReadPython ListDict
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define `result: pd.DataFrame` with `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: pd.DataFrame` z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `pd.DataFrame()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.DataFrame, \
'Variable `result` has an invalid type; expected: `pd.DataFrame`.'

>>> result  # doctest: +NORMALIZE_WHITESPACE
  firstname  lastname       role
0      Mark    Watney   botanist
1   Melissa     Lewis  commander
2      Rick  Martinez      pilot
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd

# %% Types
result: pd.DataFrame

# %% Data
DATA = [
    {'firstname': 'Mark', 'lastname': 'Watney', 'role': 'botanist'},
    {'firstname': 'Melissa', 'lastname': 'Lewis', 'role': 'commander'},
    {'firstname': 'Rick', 'lastname': 'Martinez', 'role': 'pilot'},
]

# %% Result
result = ...

# %% About
# - Name: Pandas ReadPython ListList
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define `result: pd.DataFrame` with `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: pd.DataFrame` z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `pd.DataFrame()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.DataFrame, \
'Variable `result` has an invalid type; expected: `pd.DataFrame`.'

>>> result  # doctest: +NORMALIZE_WHITESPACE
  firstname  lastname       role
0      Mark    Watney   botanist
1   Melissa     Lewis  commander
2      Rick  Martinez      pilot
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd

# %% Types
result: pd.DataFrame

# %% Data
DATA = [
    ('Mark', 'Watney', 'botanist'),
    ('Melissa', 'Lewis', 'commander'),
    ('Rick', 'Martinez', 'pilot'),
]

COLUMNS = ['firstname', 'lastname', 'role']

# %% Result
result = ...