Kurzy programovacího jazyka Python

Block size,Time to read
1,672512695
2,338152789
3,280886198
4,261732244
5,241726381
6,222869657
7,214296698
8,202491102
9,182263641
10,177141401
import pandas
df = pandas.read_csv("integer_values.csv")
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
Block size,Time to read
1,672512695
2,338152789
3,280886198
4,261732244
5,
6,222869657
7,214296698
8,202491102
9,182263641
10,177141401
float64 neboli doubleimport pandas
df = pandas.read_csv("missing_integer_values.csv")
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
NAimport pandas
df = pandas.read_csv("missing_integer_values.csv", dtype={"Time to read": "Int64"})
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
n,Timestamp
1,2020-01-15 03:59:47
2,2020-01-15 08:19:25
3,2020-01-15 11:42:07
4,2020-01-15 14:58:48
5,2020-01-15 18:21:56
6,2020-01-15 21:10:01
7,2020-01-15 23:13:58
8,2020-01-16 01:51:52
9,2020-01-16 05:55:55
10,2020-01-16 10:11:54
11,2020-01-16 14:02:32
12,2020-01-16 17:35:25
13,2020-01-16 19:35:43
14,2020-01-16 22:29:24
import pandas
df = pandas.read_csv("timestamps.csv")
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
import pandas
df = pandas.read_csv("timestamps.csv", parse_dates=["Timestamp"])
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
n,Timestamp
1,2020/01/15 03-59-47
2,2020/01/15 08-19-25
3,2020/01/15 11-42-07
4,2020/01/15 14-58-48
5,2020/01/15 18-21-56
6,2020/01/15 21-10-01
7,2020/01/15 23-13-58
8,2020/01/16 01-51-52
9,2020/01/16 05-55-55
10,2020/01/16 10-11-54
11,2020/01/16 14-02-32
12,2020/01/16 17-35-25
13,2020/01/16 19-35-43
14,2020/01/16 22-29-24
import pandas
df = pandas.read_csv("custom_timestamps.csv", parse_dates=["Timestamp"])
pandas.to_datetime(df.Timestamp)
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
datetime_parserimport pandas
import datetime
def datetime_parser(raw_data):
return datetime.datetime.strptime(raw_data, "%Y/%m/%d %H-%M-%S")
df = pandas.read_csv("custom_timestamps.csv",
date_parser=datetime_parser,
parse_dates=["Timestamp"])
pandas.to_datetime(df.Timestamp)
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
import pandas
import datetime
df = pandas.read_csv("custom_timestamps.csv",
date_parser=lambda raw_data: datetime.datetime.strptime(raw_data, "%Y/%m/%d %H-%M-%S"),
parse_dates=["Timestamp"])
pandas.to_datetime(df.Timestamp)
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
Sep 2020 Sep 2019 Change Language Ratings Changep
1 2 change C 15.95 +0.74
2 1 change Java 13.48 -3.18
3 3 Python 10.47 +0.59
4 4 C++ 7.11 +1.48
5 5 C# 4.58 +1.18
6 6 Visual Basic 4.12 +0.83
7 7 JavaScript 2.54 +0.41
8 9 change PHP 2.49 +0.62
9 19 change R 2.37 +1.33
10 8 change SQL 1.76 -0.19
11 14 change Go 1.46 +0.24
12 16 change Swift 1.38 +0.28
13 20 change Perl 1.30 +0.26
14 12 change Assembly language 1.30 -0.08
15 15 Ruby 1.24 +0.03
16 18 change MATLAB 1.10 +0.04
17 11 change Groovy 0.99 -0.52
18 33 change Rust 0.92 +0.55
19 10 change Objective-C 0.85 -0.99
20 24 change Dart 0.77 +0.13
pandas.read_csv není tento formát správně rozpoznánimport pandas
df = pandas.read_csv("tiobe.tsv")
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
sep specifikujeme oddělovač mezi záznamyimport pandas
# separator/delimiter specification
df = pandas.read_csv("tiobe.tsv", sep="\t")
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
| relativně často se jedná o středníky, dvojtečky nebo o znak |
Sep 2020 Sep 2019 Change Language Ratings Changep
1 2 change C 15.95 +0.74
2 1 change Java 13.48 -3.18
3 3 Python 10.47 +0.59
4 4 C++ 7.11 +1.48
5 5 C# 4.58 +1.18
6 6 Visual Basic 4.12 +0.83
7 7 JavaScript 2.54 +0.41
8 9 change PHP 2.49 +0.62
9 19 change R 2.37 +1.33
10 8 change SQL 1.76 -0.19
11 14 change Go 1.46 +0.24
12 16 change Swift 1.38 +0.28
13 20 change Perl 1.30 +0.26
14 12 change Assembly language 1.30 -0.08
15 15 Ruby 1.24 +0.03
16 18 change MATLAB 1.10 +0.04
17 11 change Groovy 0.99 -0.52
18 33 change Rust 0.92 +0.55
19 10 change Objective-C 0.85 -0.99
20 24 change Dart 0.77 +0.13
NaNimport pandas
# separator/delimiter specification not needed there
df = pandas.read_fwf("tiobe.txt")
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
widths, kde šířky nastavímeimport pandas
# separator/delimiter specification not needed there
df = pandas.read_fwf("tiobe.txt", widths=(20, 20, 20, 20, 20, 20))
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
20.11.2020 #224
země|měna|množství|kód|kurz
Austrálie|dolar|1|AUD|16,231
Brazílie|real|1|BRL|4,160
Bulharsko|lev|1|BGN|13,467
Čína|žen-min-pi|1|CNY|3,381
Dánsko|koruna|1|DKK|3,536
EMU|euro|1|EUR|26,340
Filipíny|peso|100|PHP|46,038
Hongkong|dolar|1|HKD|2,864
Chorvatsko|kuna|1|HRK|3,481
Indie|rupie|100|INR|29,950
Indonesie|rupie|1000|IDR|1,567
Island|koruna|100|ISK|16,330
Izrael|nový šekel|1|ILS|6,649
Japonsko|jen|100|JPY|21,383
Jižní Afrika|rand|1|ZAR|1,445
Kanada|dolar|1|CAD|17,011
Korejská republika|won|100|KRW|1,990
Maďarsko|forint|100|HUF|7,328
Malajsie|ringgit|1|MYR|5,425
Mexiko|peso|1|MXN|1,104
MMF|ZPČ|1|XDR|31,598
Norsko|koruna|1|NOK|2,471
Nový Zéland|dolar|1|NZD|15,416
Polsko|zlotý|1|PLN|5,900
Rumunsko|leu|1|RON|5,405
Rusko|rubl|100|RUB|29,180
Singapur|dolar|1|SGD|16,530
Švédsko|koruna|1|SEK|2,577
Švýcarsko|frank|1|CHF|24,363
Thajsko|baht|100|THB|73,313
Turecko|lira|1|TRY|2,911
USA|dolar|1|USD|22,201
Velká Británie|libra|1|GBP|29,464
import pandas
df = pandas.read_csv("denni_kurz.txt")
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
sep předaný funkci pandas.read_csvimport pandas
df = pandas.read_csv("denni_kurz.txt", sep="|")
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
skiprows
sepimport pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
pandas.to_numericimport pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
import pandas
url = "https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt"
df = pandas.read_csv(url, sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print("Data frame")
print("---------------------------")
print(df)
print()
print("Column types")
print("---------------------------")
print(df.dtypes)
Series odvozený od jednodimenzionálního pole knihovny NumpyDataFrame# Datový typ Stručný popis
1 Series odvozeno od 1D pole knihovny Numpy, rozšířeno o popis os
2 DataFrame reprezentace dat uložených do tabulky s popisem os (sloupců, řádků)
3 DatetimeTZDtype datum s přidanou informací o časové zóně
4 PeriodDtype reprezentace časové periody (offsetu)
5 IntervalDtype reprezentace numerického intervalu (odvozeno od dalších typů, například int64 atd.)
6 Int8Dtype typ int8 rozšířený pro podporu hodnoty pandas.NA
7 Int16Dtype typ int16 rozšířený pro podporu hodnoty pandas.NA
8 Int32Dtype typ int32 rozšířený pro podporu hodnoty pandas.NA
9 Int64Dtype typ int64 rozšířený pro podporu hodnoty pandas.NA
10 UInt8Dtype typ uint8 rozšířený pro podporu hodnoty pandas.NA
11 UInt16Dtype typ uint16 rozšířený pro podporu hodnoty pandas.NA
12 UInt32Dtype typ uint32 rozšířený pro podporu hodnoty pandas.NA
13 UInt64Dtype typ uint64 rozšířený pro podporu hodnoty pandas.NA
14 CategoricalDtype kategorie (odvozeno od jazyka R, bude popsáno příště)
15 SparseDtype použito pro ukládání řídkých polí (bude popsáno příště)
16 StringDtype rozšíření řetězců; prozatím ve fázi experimentálního rozšíření
17 BooleanDtype rozšíření pravdivostního typu; prozatím ve fázi experimentálního rozšíření
import pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print(df)
import pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print(df.head())
df.typesimport pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print(df.dtypes)
df.columnsimport pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print(df.columns)
df.infoimport pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print(df.info())
import pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print("Axes: ", df.axes)
print("Ndim: ", df.ndim)
print("Size: ", df.size)
print("Shape: ", df.shape)
describe lze získat základní (a mnohdy velmi užitečné) statistické informace o záznamech uložených v datovém rámciimport pandas
df = pandas.read_csv("denni_kurz.txt", sep="|", skiprows=1)
df["kurz"] = pandas.to_numeric(df["kurz"].str.replace(',','.'), errors='coerce')
print(df.describe())
import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Check if command line argument is specified (it is mandatory).
if len(sys.argv) < 2:
print("Usage:")
print(" plot_kafka_lags_pandas.py input_file.csv")
print("Example:")
print(" plot_kafka_lags_pandas.py overall.csv")
sys.exit(1)
# First command line argument should contain name of input CSV.
input_csv = sys.argv[1]
df = pd.read_csv(input_csv)
print(df.info())
print(df.describe())
# Linear regression
time = df["Time"]
messages = df["topic : uploads"]
# Linear regression
x = np.arange(0, len(messages))
coef = np.polyfit(x, messages, 1)
poly1d_fn = np.poly1d(coef)
# Create new graph
plt.plot(messages, "b", poly1d_fn(np.arange(0, len(messages))), 'y--')
# Title of a graph
plt.title("Messages in Kafka")
# Add a label to x-axis
plt.xlabel("Time")
# Add a label to y-axis
plt.ylabel("Messages")
plt.legend(loc="upper right")
# Set the plot layout
plt.tight_layout()
# And save the plot into raster format and vector format as well
plt.savefig("kafka_lags_pandas.png")
plt.savefig("kafka_lags_pandas.svg")
# Try to show the plot on screen
plt.show()
import sys
import pandas as pd
import matplotlib.pyplot as plt
# Check if command line argument is specified (it is mandatory).
if len(sys.argv) < 2:
print("Usage:")
print(" plot_kafka_lags_pandas_2.py input_file.csv")
print("Example:")
print(" plot_kafka_lags_pandas_2.py overall.csv")
sys.exit(1)
# First command line argument should contain name of input CSV.
input_csv = sys.argv[1]
df = pd.read_csv(input_csv)
print(df.info())
print(df.describe())
# Create new histogram graph
df.plot(x="Time", y="topic : uploads")
# Try to show the plot on screen
plt.show()
df.shape, popř. df.iloc:import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Check if command line argument is specified (it is mandatory).
if len(sys.argv) < 2:
print("Usage:")
print(" plot_kafka_lags_pandas_sma_3.py input_file.csv")
print("Example:")
print(" plot_kafka_lags_pandas_sma_3.py overall.csv")
sys.exit(1)
# First command line argument should contain name of input CSV.
input_csv = sys.argv[1]
df = pd.read_csv(input_csv)
for i in range(0, df.shape[0]-2):
df.loc[df.index[i+2], 'SMA_3'] = np.round(((df.iloc[i,1]+ df.iloc[i+1,1] +df.iloc[i+2,1])/3),1)
print(df)
print(df.info())
print(df.describe())
# Create new histogram graph
df.plot(x="Time", y=["topic : uploads", "SMA_3"])
# Try to show the plot on screen
plt.show()
rolling a meanimport sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Check if command line argument is specified (it is mandatory).
if len(sys.argv) < 2:
print("Usage:")
print(" plot_kafka_lags_pandas_sma_3.py input_file.csv")
print("Example:")
print(" plot_kafka_lags_pandas_sma_3.py overall.csv")
sys.exit(1)
# First command line argument should contain name of input CSV.
input_csv = sys.argv[1]
df = pd.read_csv(input_csv)
df['SMA_3'] = df.iloc[:,1].rolling(window=3).mean()
print(df)
print(df.info())
print(df.describe())
# Create new histogram graph
df.plot(x="Time", y=["topic : uploads", "SMA_3"])
# Try to show the plot on screen
plt.show()
df.plot.bar()#!/usr/bin/env python3
import sys
import pandas as pd
import matplotlib.pyplot as plt
# Check if command line argument is specified (it is mandatory).
if len(sys.argv) < 2:
print("Usage:")
print(" plot_benchmark_results_bar_chart.py ")
print("Example:")
print(" plot_benchmark_results_bar_chart.py data.tsv")
sys.exit(1)
# First command line argument should contain name of input CSV.
input_file = sys.argv[1]
df = pd.read_csv(input_file, sep="\t")
data_columns = ["ANSI C", "Cython #1", "Cython #2", "Cython #3", "Numba #1/interpret", "Numba #2", "Numba #3", "Numba #4"]
for data_column in data_columns:
df[data_column] = pd.to_numeric(df[data_column].str.replace(',', '.'), errors='coerce')
print(df)
print()
print(df.info())
print()
print(df.describe())
print()
# Create new histogram graph
df.plot.bar(x="Height", y=data_columns)
# Try to show the plot on screen
plt.show()
Series (datová řada)
Series mají několik užitečných atributů# Atribut Stručný popis
1 index indexy prvků v řadě
2 values hodnoty prvků ve formě 1D pole
3 size počet prvků v řadě
4 name jméno řady (pokud je specifikováno)
5 dtype typ prvků uložených v datové řadě
6 hasnans test, zda je nějaký prvek roven NaN
import pandas
s = pandas.Series((1, 2, 3, 4, 5, 6))
print("Series:")
print(s)
print()
print("Index:")
print(s.index)
print()
print("Values:")
print(s.values)
print()
import pandas
s = pandas.Series(('a', 'b', 'c', 'd', 'e', 'f'), (1, 2, 3, 4, 5, 6))
print("Series:")
print(s)
print()
print("Index:")
print(s.index)
print()
print("Values:")
print(s.values)
print()
import pandas
input_data = {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
}
print("Input data:")
print(input_data)
print()
s = pandas.Series(input_data)
print("Series:")
print(s)
print()
print("Index:")
print(s.index)
print()
print("Values:")
print(s.values)
print()
import pandas
from collections import OrderedDict
input_data = OrderedDict()
input_data["f"] = 6
input_data["e"] = 5
input_data["d"] = 4
input_data["c"] = 3
input_data["b"] = 2
input_data["a"] = 1
print("Input data:")
print(input_data)
print()
s = pandas.Series(input_data)
print("Series:")
print(s)
print()
print("Index:")
print(s.index)
print()
print("Values:")
print(s.values)
print()
pandas.Series.reindex
import pandas
s = pandas.Series(range(1, 7), ('a', 'b', 'c', 'd', 'e', 'f'))
print("Series:")
print(s)
print()
print("Index:")
print(s.index)
print()
print("Values:")
print(s.values)
print()
s = s.reindex(['d', 'a', 'b', 'c', 'd', 'a', 'a', 'a'])
print("Reindexed:")
print()
print("Series:")
print(s)
print()
print("Index:")
print(s.index)
print()
print("Values:")
print(s.values)
print()
import pandas
s = pandas.Series(range(1, 7), ('a', 'b', 'c', 'd', 'e', 'f'))
print("sum", s.sum(), sep="\t")
print("prod", s.prod(), sep="\t")
print("min", s.min(), sep="\t")
print("max", s.max(), sep="\t")
print("median", s.median(), sep="\t")
print("std", s.std(), sep="\t")
print("var", s.var(), sep="\t")
print("quantile", s.quantile(0.01), sep="\t")
import pandas
s = pandas.Series((1, 2, None, 4, 5, 6), ('a', 'b', 'c', 'd', 'e', 'f'))
print("sum", s.sum(), sep="\t")
print("prod", s.prod(), sep="\t")
print("min", s.min(), sep="\t")
print("max", s.max(), sep="\t")
print("median", s.median(), sep="\t")
print("std", s.std(), sep="\t")
print("var", s.var(), sep="\t")
print("quantile", s.quantile(0.01), sep="\t")
import numpy as np
import pandas
s1 = pandas.Series(range(1, 7), ('a', 'b', 'c', 'd', 'e', 'f'))
print(s1 + 10)
print(s1 - 10)
print(s1 * 10)
print(s1 / 10)
print(s1 % 2)
True a Falseimport numpy as np
import pandas
s1 = pandas.Series(range(1, 7), ('a', 'b', 'c', 'd', 'e', 'f'))
print(s1 > 3)
print(s1 < 3)
print(s1 % 2 == 0)
import numpy as np
import pandas
s1 = pandas.Series((1, 2, None, 4, 5, 6), ('a', 'b', 'c', 'd', 'e', 'f'))
print(s1 > 3)
print(s1 < 3)
print(s1 % 2 == 0)
True, bude prvek použit ve výsledku, v opačném případě bude zahozenimport numpy as np
import pandas
s1 = pandas.Series(range(1, 7))
s2 = pandas.Series(range(50))
print(s1[s1<3])
print(s1[s1>3])
print(s2[s2 % 2 == 0])
print(s2[s2 % 2 != 0])
print(s2[s2 % 3 == 0])
import numpy as np
import pandas
s1 = pandas.Series(range(1, 7))
s2 = pandas.Series(range(-3, 3))
print(s1[s2 >= 0])
print(s1[s2 < 0])
print(s1[s2 != 0])
import pandas
s = pandas.Series((100, 200, 300, 400, 500, 600))
print(s.dtypes)
print(s)
print()
s = s.astype('int32')
print(s.dtypes)
print(s)
print()
s =s.astype('int8')
print(s.dtypes)
print(s)
import pandas
s = pandas.Series((100, 200, 300, 400, 500, 600), dtype="float32")
print(s.dtypes)
print(s)
print()
s = s.convert_dtypes()
print(s.dtypes)
print(s)
print()
# Metoda Stručný popis
1 pandas.Series.plot graf vybraný parametrem kind
2 pandas.Series.plot.area oblast pod průběhem je vyplněna barvou (pro kladné hodnoty)
3 pandas.Series.plot.bar sloupcový graf s vertikálně orientovanými sloupci
4 pandas.Series.plot.barh sloupcový graf s horizontálně orientovanými sloupci
5 pandas.Series.plot.box krabicový diagram
6 pandas.Series.plot.density diagram založený na KDE
7 pandas.Series.plot.hist histogram
8 pandas.Series.plot.kde diagram založený na KDE
9 pandas.Series.plot.line stejné jako pandas.Series.plot
10 pandas.Series.plot.pie koláčový diagram
11 pandas.Series.hist histogram
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
# tisk obsahu Series
print(s)
# vytvoření grafu
s.plot()
# uložení grafu
plt.savefig("series_plot_01.png")
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
# tisk obsahu Series
print(s)
# vytvoření grafu
s.plot.area(stacked=False)
# uložení grafu
plt.savefig("series_plot_02.png")
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 20)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
# tisk obsahu Series
print(s)
# vytvoření grafu
s.plot.bar(grid=True)
# uložení grafu
plt.savefig("series_plot_03.png")
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 20)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
# tisk obsahu Series
print(s)
# vytvoření grafu
s.plot.barh(grid=True)
# uložení grafu
plt.savefig("series_plot_04.png")
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
# tisk obsahu Series
print(s)
# vytvoření grafu
s.plot.kde(grid=True)
# vykreslení grafu
plt.show()
Sep 2020 Sep 2019 Change Language Ratings Changep
1 2 change C 15.95 +0.74
2 1 change Java 13.48 -3.18
3 3 Python 10.47 +0.59
4 4 C++ 7.11 +1.48
5 5 C# 4.58 +1.18
6 6 Visual Basic 4.12 +0.83
7 7 JavaScript 2.54 +0.41
8 9 change PHP 2.49 +0.62
9 19 change R 2.37 +1.33
10 8 change SQL 1.76 -0.19
11 14 change Go 1.46 +0.24
12 16 change Swift 1.38 +0.28
13 20 change Perl 1.30 +0.26
14 12 change Assembly language 1.30 -0.08
15 15 Ruby 1.24 +0.03
16 18 change MATLAB 1.10 +0.04
17 11 change Groovy 0.99 -0.52
18 33 change Rust 0.92 +0.55
19 10 change Objective-C 0.85 -0.99
20 24 change Dart 0.77 +0.13
import numpy as np
import pandas
import matplotlib.pyplot as plt
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce struktury Series - datové řady z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu Series
print(s)
# vytvoření grafu
s.plot.pie()
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce datové struktury Series
s = pandas.Series(data=np.sin(r), index=r)
# šum
s2 = np.random.rand(100)/2
# přidání šumu k původní řadě
s3 = s + s2
# tisk obsahu Series
print(s3)
# vytvoření grafu
s3.plot(grid=True)
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
# přidání šumu
s2 = s.map(lambda x: x+np.random.rand()/2)
# vyhlazení (moving average)
s3 = s2.rolling(2).mean()
# tisk obsahu Series
print(s3)
# vytvoření grafu
s3.plot(grid=True)
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
# přidání šumu
s2 = s.map(lambda x: x+np.random.rand()/2)
# vyhlazení (moving average)
s3 = s2.rolling(20).mean()
# tisk obsahu Series
print(s3)
# vytvoření grafu
s3.plot(grid=True)
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
s2 = s.map(lambda x: x+np.random.rand()/2)
s3 = s2.rolling(10, win_type="gaussian").sum(std=3)
# tisk obsahu Series
print(s3)
# vytvoření grafu
s3.plot(grid=True)
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
s2 = s.map(lambda x: x+np.random.rand()/2)
s3 = s2 - s
# vytvoření grafu
plt.plot(s, "--", s2, "-", s3, "-")
# uložení grafu
plt.savefig("series_plot_11.png")
# vykreslení grafu
plt.show()
import numpy as np
import pandas
import matplotlib.pyplot as plt
# hodnoty na x-ové ose
r = np.linspace(0, 2*np.pi, 100)
# konstrukce struktury Series - datové řady
s = pandas.Series(data=np.sin(r), index=r)
s2 = s.map(lambda x: x+np.random.rand()/2)
s3 = s2 - s
# vytvoření grafu
plt.subplot(221)
plt.plot(s)
plt.subplot(222)
plt.plot(s2)
plt.subplot(223)
plt.plot(s3)
# uložení grafu
plt.savefig("series_plot_12.png")
# vykreslení grafu
plt.show()
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu Series
print(s)
# iterace nad prvky rady
for index, value in s.items():
print("Index: {:20} Value: {:5.3}".format(index, value))
iteritemsimport pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu Series
print(s)
# iterace nad prvky rady
for index, value in s.iteritems():
print("Index: {:20} Value: {:5.3}".format(index, value))
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print("In percents")
print(s)
print()
# převod na skutečný poměr <0, 1>
s2 = s.map(lambda x: x/100.0)
# tisk obsahu nové datové řady
print("As ratios")
print(s2)
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print("In percents")
print(s)
print()
# formát hodnot v datové řadě
s2 = s.map("Rating is {:4.1f} %".format)
# tisk obsahu nové datové řady
print("As ratings")
print(s2)
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print("In percents")
print(s)
print()
# převod na skutečný poměr <0, 1>
s2 = s.transform(lambda x: x/100.0)
# tisk obsahu nové datové řady
print("As ratios")
print(s2)
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print("In percents")
print(s)
print()
# převod na skutečný poměr <0, 1>
s2 = s.transform([lambda x: x, lambda x: x/100.0])
# tisk obsahu nové datové řady
print("In percents and also as ratios")
print(s2)
import pandas
import numpy as np
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print("In percents")
print(s)
print()
# agregace výsledků
results = s.aggregate([np.min, np.max, np.sum, np.mean])
# tisk výsledku
print("Results")
print(results)
min a maximport pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print(s)
print()
# omezení hodnot
results = s.combine(10, min)
# tisk výsledku
print("Bound (max)")
print(results)
# omezení hodnot
results = results.combine(2, max)
# tisk výsledku
print("Bound (min)")
print(results)
Series.maskNA nebo NaN
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print(s)
print()
# maskování hodnot
results = s.mask(s > 10)
# tisk výsledku
print("Masked (max)")
print(results)
# maskování hodnot
results = results.mask(s < 2)
# tisk výsledku
print("Masked (min)")
print(results)
Series.whereSeries.maskimport pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print(s)
print()
# maskování hodnot
results = s.where(s < 10, "Dobře")
# tisk výsledku
print(results)
# maskování hodnot
results = results.where(s > 2, "Nic moc")
# tisk výsledku
print(results)
Series.where a Series.dropnaSeries.dropna:import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print(s)
print()
# maskování hodnot
results = s.where(s < 10)
# tisk výsledku
print(results)
results = results.dropna()
# tisk nového výsledku
print(results)
Series.filterimport pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# pro jistotu si datový rámec zobrazíme
print(df)
print()
# konstrukce datové struktury Series (datové řady) z datového rámce
s = pandas.Series(df["Ratings"])
# tisk obsahu původní datové řady
print(s)
print()
# maskování hodnot
results = s.filter(regex="C.*")
# tisk výsledku
print(results)
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# převod na skutečný poměr <0, 1>
df["Ratings as ratio"] = df["Ratings"].map(lambda x: x/100.0)
# datový rámec zobrazíme
print(df)
print()
# podrobnější informace o datovém rámci
print(df.dtypes)
print()
# více podrobnějších informací o datovém rámci
print(df.info())
print()
#!/usr/bin/env python3
# vim: set fileencoding=utf-8
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# převod na skutečný poměr <0, 1>
df["Ratings"] = df["Ratings"].map(lambda x: x/100.0)
# datový rámec zobrazíme
print(df)
print()
# podrobnější informace o datovém rámci
print(df.dtypes)
print()
# více podrobnějších informací o datovém rámci
print(df.info())
print()
#!/usr/bin/env python3
# vim: set fileencoding=utf-8
import pandas
# přečtení zdrojových dat
df = pandas.read_csv("tiobe.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df.set_index("Language", inplace=True)
# formát hodnot ve sloupci
df["Ratings"] = df["Ratings"].map("Rating is {:4.1f}%".format)
# datový rámec zobrazíme
print(df)
print()
# podrobnější informace o datovém rámci
print(df.dtypes)
print()
# více podrobnějších informací o datovém rámci
print(df.info())
print()
Různé způsoby spojení rámců:
appendimport pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeC.tsv", sep="\t")
df2 = pandas.read_csv("tiobeD.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df1.set_index("Language", inplace=True)
df2.set_index("Language", inplace=True)
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
concatenated = df1.append(df2)
# výpis výsledku
print(concatenated)
concatconcatDataFrameaxis, který by měl obsahovat hodnotu 0 nebo 1 (popř. nebýt vůbec uveden)import pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeE.tsv", sep="\t")
df2 = pandas.read_csv("tiobeF.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df1.set_index("Language", inplace=True)
df2.set_index("Language", inplace=True)
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
concatenated = pandas.concat([df1, df2], axis=1)
# výpis výsledku
print(concatenated)
import pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeC.tsv", sep="\t")
df2 = pandas.read_csv("tiobeD.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df1.set_index("Language", inplace=True)
df2.set_index("Language", inplace=True)
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
concatenated = pandas.concat([df1, df2])
# výpis výsledku
print(concatenated)
mergemergeimport pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeA.tsv", sep="\t")
df2 = pandas.read_csv("tiobeB.tsv", sep="\t")
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
merged = pandas.merge(df1, df2)
# výpis výsledku
print(merged)
import pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeA.tsv", sep="\t")
df2 = pandas.read_csv("tiobeB.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df1.set_index("Language", inplace=True)
df2.set_index("Language", inplace=True)
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
merged = pandas.merge(df1, df2)
# výpis výsledku
print(merged)
import pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeA.tsv", sep="\t")
df2 = pandas.read_csv("tiobeB.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df1.set_index("Language", inplace=True)
df2.set_index("Language", inplace=True)
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
merged = pandas.merge(df1, df2, left_index=True, right_index=True,
on=["Change", "Ratings", "Changep"])
# výpis výsledku
print(merged)
mergeimport pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeA.tsv", sep="\t")
df2 = pandas.read_csv("tiobeB.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df1.set_index("Language", inplace=True)
df2.set_index("Language", inplace=True)
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
merged = pandas.merge(df1, df2, left_index=True, right_index=True,
how="left",
on=["Change", "Ratings", "Changep"])
# výpis výsledku
print(merged)
mergeimport pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeA.tsv", sep="\t")
df2 = pandas.read_csv("tiobeB.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df1.set_index("Language", inplace=True)
df2.set_index("Language", inplace=True)
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
merged = pandas.merge(df1, df2, left_index=True, right_index=True,
how="right",
on=["Change", "Ratings", "Changep"])
# výpis výsledku
print(merged)
mergeimport pandas
# přečtení zdrojových dat
df1 = pandas.read_csv("tiobeA.tsv", sep="\t")
df2 = pandas.read_csv("tiobeB.tsv", sep="\t")
# specifikace indexu - má se získat ze sloupce Language
df1.set_index("Language", inplace=True)
df2.set_index("Language", inplace=True)
# datové rámce zobrazíme
print(df1)
print()
print(df2)
print()
# spojení obou datových rámců
merged = pandas.merge(df1, df2, left_index=True, right_index=True,
how="outer",
on=["Change", "Ratings", "Changep"])
# výpis výsledku
print(merged)
groupby, naformátování a export tabulek pro tiskgroupby