Balíčky

Intermediate Python for Developers

Jasmin Ludolf

Senior Data Science Content Developer

Moduly jsou soubory Pythonu

  • Modul = soubor Pythonu

  • Soubor Pythonu může vytvořit kdokoli!

Soubor kódu na laptopu

Intermediate Python for Developers

Balíčky

  • Kolekce modulů = balíček
    • Také nazývaný knihovna
  • Veřejně dostupný a zdarma
  • Stažení z PyPI
  • Poté lze importovat a používat jako moduly

Velká kartonová krabice

1 https://pypi.org/
Intermediate Python for Developers

Instalace balíčku

  • Terminál / příkazový řádek

    python3 -m pip install <package_name>
    
  • python3 – spouští kód Pythonu z terminálu

  • pip – preferovaný instalátor

Terminál s kódem

Intermediate Python for Developers

Instalace balíčku

 

python3 -m pip install pandas

Logo Pandas

$$

  • Balíček pro manipulaci a analýzu dat
Intermediate Python for Developers

Import s aliasem

# Import pandas
import pandas
  • Použijte alias pro zkrácení kódu
# Import pandas using an alias
import pandas as pd
Intermediate Python for Developers

Vytvoření DataFrame

# Sales dictionary
sales = {"user_id": ["KM37", "PR19", "YU88"],
         "order_value": [197.75, 208.21, 134.99]}

# Convert to a pandas DataFrame sales_df = pd.DataFrame(sales)
print(sales_df)
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
Intermediate Python for Developers

Načtení souboru CSV

# Reading in a CSV file in our current directory
sales_df = pd.read_csv("sales.csv")

# Checking the data type print(type(sales_df))
pandas.core.frame.DataFrame
Intermediate Python for Developers

Náhled souboru

# DataFrame method to preview the first five rows
print(sales_df.head())
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
3    NT43       153.54        
4    IW06       379.47
Intermediate Python for Developers

Informace o souboru

# Checking the file info
print(sales_df.info())
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
 #   Column       Non-Null Count  Dtype  
<hr />  ------       --------------  -----  
 0   user_id      3 non-null      object 
 1   order_value  3 non-null      float64
dtypes: float64(1), object(1)
memory usage: 180.0+ bytes
Intermediate Python for Developers

Funkce vs. metody

# This is a built-in function
print(sum([1, 2 ,3, 4, 5]))
15
  • Funkce = kód pro provedení úkolu
# This is a pandas function
sales_df = pd.DataFrame(sales)
  • .head() funguje pouze s pandas DataFrames
# This is a method
print(sales_df.head())
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
3    NT43       153.54        
4    IW06       379.47
  • Metoda = funkce specifická pro datový typ
Intermediate Python for Developers

Pojďme si procvičit!

Intermediate Python for Developers

Preparing Video For Download...