import pandas as pd
from matplotlib import pyplot as plt
Iterating over column, rows, etc
Iterating in python
for a in ['a', 'b', 'c']:
print(a)
a
b
c
for i, a in enumerate(['a', 'b', 'c']):
print(i, a)
0 a
1 b
2 c
= pd.read_csv('rates.csv', parse_dates=['Time'])
df = df['Time']
df.index df.head()
Time | USD | JPY | BGN | CZK | DKK | GBP | CHF | |
---|---|---|---|---|---|---|---|---|
Time | ||||||||
2024-01-17 | 2024-01-17 | 1.0877 | 160.65 | 1.9558 | 24.755 | 7.4586 | 0.85818 | 0.9406 |
2024-01-16 | 2024-01-16 | 1.0882 | 159.64 | 1.9558 | 24.710 | 7.4582 | 0.86078 | 0.9361 |
2024-01-15 | 2024-01-15 | 1.0945 | 159.67 | 1.9558 | 24.714 | 7.4590 | 0.86075 | 0.9351 |
2024-01-12 | 2024-01-12 | 1.0942 | 159.17 | 1.9558 | 24.689 | 7.4565 | 0.85950 | 0.9350 |
2024-01-11 | 2024-01-11 | 1.0987 | 159.71 | 1.9558 | 24.659 | 7.4568 | 0.86145 | 0.9338 |
Iterating over column names of a dataframe
for col in df:
print(col)
Time
USD
JPY
BGN
CZK
DKK
GBP
CHF
for col in df.columns:
print(col)
Time
USD
JPY
BGN
CZK
DKK
GBP
CHF
Iterating over rows of a dataframe, with each row as serie
for idx, row in df.iterrows():
print("index is", idx)
print()
assert isinstance(row, pd.Series)
print(row)
break
index is 2024-01-17 00:00:00
Time 2024-01-17 00:00:00
USD 1.0877
JPY 160.65
BGN 1.9558
CZK 24.755
DKK 7.4586
GBP 0.85818
CHF 0.9406
Name: 2024-01-17 00:00:00, dtype: object
itertuples: iterating over rows as named tuples
= [1, 2, 3]
l 0]= 4
l[ l
[4, 2, 3]
= (1, 2, 3)
t 0] = 4 t[
TypeError: 'tuple' object does not support item assignment
for row in df.itertuples():
assert isinstance(row, tuple)
print('index is', row.Index)
print('USD rate is', row.USD)
print("JPY rate is", row.JPY)
break
index is 2024-01-17 00:00:00
USD rate is 1.0877
JPY rate is 160.65
Iterating over columns to get the corresponding series
= {'c1': "a", 'c2': 'b', 'c3': 'c'}
d for k, v in d.items():
print('key', k, 'value is', d[k])
key c1 value is a
key c2 value is b
key c3 value is c
'USD'] df[
Time
2024-01-17 1.0877
2024-01-16 1.0882
2024-01-15 1.0945
2024-01-12 1.0942
2024-01-11 1.0987
...
2023-10-26 1.0540
2023-10-25 1.0576
2023-10-24 1.0632
2023-10-23 1.0597
2023-10-20 1.0591
Name: USD, Length: 61, dtype: float64
for column_label, column_as_serie in df.items():
print(column_label)
print(column_as_serie)
break
Time
Time
2024-01-17 2024-01-17
2024-01-16 2024-01-16
2024-01-15 2024-01-15
2024-01-12 2024-01-12
2024-01-11 2024-01-11
...
2023-10-26 2023-10-26
2023-10-25 2023-10-25
2023-10-24 2023-10-24
2023-10-23 2023-10-23
2023-10-20 2023-10-20
Name: Time, Length: 61, dtype: datetime64[ns]
type(column_as_serie)
pandas.core.series.Series
Do not modify objects you are iterating over
= pd.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]})
df df
a | b | |
---|---|---|
0 | 1 | a |
1 | 2 | b |
2 | 3 | c |
for index, row in df.iterrows():
"a"] = 10 row[
df
a | b | |
---|---|---|
0 | 1 | a |
1 | 2 | b |
2 | 3 | c |